Home > Back-end >  Please help change keys nested inside dictionary
Please help change keys nested inside dictionary

Time:05-10

Hello I am beginner in javascript it would be great if you could help me

topicMessage = { topic: 'some_topic', messages: [{ 'myKey': '{"data": {"my name is ":"aman", "age": 22, "my ed": "btech"}, "meta": {"myAge":24, "school":"aps"}}' }, { 'myKey2': '{ "data": { "my name is 2": "aman", "age": 22, "my ed 2": "btech" }, "meta": { "myAge2": 24, "school": "aps" } } ' }, { "myKey3": '{"data": {"my name is 3":"aman", "age": 22, "my ed 3": "btech"}, "meta": {"myAge":24, "school":"aps"}}' } ] }

here inside messaages of topicMessages in data part we have to make keys dash separated, and value for mykey, mykey2, mykey3 are in string form, so need to convert JSON.parse to object first and then again convert it back to string, can somebody please help, eg for messages in for value of myKey, which is a string inside that for key, data, value which is again an object, here key "my name is" becomes "my-name-is", "my ed" becomes "my-ed"

I am trying to do using this,

for(let i = 0; i < topicMessages.messages.length; i  ){
    for(const [key, value] of Object.entries(topicMessages.messages[i])){
        for(const [key1, value1] of Object.entries(topicMessages.messages[i])){
            if(key1.includes(" ")){
                const newKey = key1.replace(/\s /g, "-")
                topicMessages.messages[i][key]["data"][newkey] = topicMessages.messages[i][key]["data"][key1];
                delete topicMessages.messages[i][key]["data"][key1];         
}
}
}
}

expected output:

topicMessage = { topic: 'some_topic', messages: [{ 'myKey': '{"data": {"my-name-is ":"aman", "age": 22, "my-ed": "btech"}, "meta": {"myAge":24, "school":"aps"}}' }, { 'myKey2': '{ "data": { "my-name-is-2": "aman", "age": 22, "my-ed-2": "btech" }, "meta": { "myAge2": 24, "school": "aps" } } ' }, { "myKey3": '{"data": {"my-name-is-3":"aman", "age": 22, "my-ed-3": "btech"}, "meta": {"myAge":24, "school":"aps"}}' } ] }

topicMessages = {
  topic: 'some_topic',
  messages: [{
    'myKey': '{"data": {"my name is ":"aman", "age": 22, "my ed": "btech"}, "meta": {"myAge":24, "school":"aps"}}'
  }, {
    'myKey2': '{ "data": { "my name is 2": "aman", "age": 22, "my ed 2": "btech" }, "meta": { "myAge2": 24, "school": "aps" } } '
  }, {
    "myKey3": '{"data": {"my name is 3":"aman", "age": 22, "my ed 3": "btech"}, "meta": {"myAge":24, "school":"aps"}}'
  }]
}

for (let i = 0; i < topicMessages.messages.length; i  ) {
  for (const [key, value] of Object.entries(topicMessages.messages[i])) {
    for (const [key1, value1] of Object.entries(topicMessages.messages[i])) {
      if (key1.includes(" ")) {
        const newKey = key1.replace(/\s /g, "-")
        topicMessages.messages[i][key]["data"][newkey] = topicMessages.messages[i][key]["data"][key1];
        delete topicMessages.messages[i][key]["data"][key1];
      }
    }
  }
}

console.log(topicMessages)

CodePudding user response:

It came out pretty ugly but it works

take a look

const topicMessage = {
  topic: 'some_topic',
  messages: [{
    'myKey': '{"data": {"my name is ":"aman", "age": 22, "my ed": "btech"}, "meta": {"myAge":24, "school":"aps"}}'
  }, {
    'myKey2': '{ "data": { "my name is 2": "aman", "age": 22, "my ed 2": "btech" }, "meta": { "myAge2": 24, "school": "aps" } } '
  }, {
    "myKey3": '{"data": {"my name is 3":"aman", "age": 22, "my ed 3": "btech"}, "meta": {"myAge":24, "school":"aps"}}'
  }]
}


const renameKeys = (data) =>
  data.map(o =>
    Object.keys(o).reduce((res, k) => {
      const json = o[k]
      const {data, ...rest} = JSON.parse(json)

      return {
        ...res,
        [k]: JSON.stringify({
          ...rest,
          data: Object.keys(data).reduce((res, key) => {
            return {
              ...res,
              [key.replace(/\s /g, "-")]: data[key]
            }
          }, {})
        })
      }
    }, {})
  )





topicMessage.messages = renameKeys(topicMessage.messages)

console.log(topicMessage)

  • Related