请帮助更改嵌套在字典中的键
Please help change keys nested inside dictionary
你好,我是javascript的初学者,如果你能帮助我就太好了
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的messaages里面我们要让keys用dash分隔,mykey,mykey2,mykey3的value都是字符串形式的,所以需要先把JSON.parse转成object再转它回到字符串,有人可以帮忙吗,
例如,对于 myKey 的值的消息,它是一个字符串,其中的键、数据、值又是一个对象,这里键“我的名字是”变成了“我的名字是”,“我的 ed”变成了“我的” -ed
我正在尝试使用它,
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];
}
}
}
}
预期输出:
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)
它出来的时候很丑,但它确实有效
看看
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)
你好,我是javascript的初学者,如果你能帮助我就太好了
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的messaages里面我们要让keys用dash分隔,mykey,mykey2,mykey3的value都是字符串形式的,所以需要先把JSON.parse转成object再转它回到字符串,有人可以帮忙吗, 例如,对于 myKey 的值的消息,它是一个字符串,其中的键、数据、值又是一个对象,这里键“我的名字是”变成了“我的名字是”,“我的 ed”变成了“我的” -ed
我正在尝试使用它,
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];
}
}
}
}
预期输出:
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)
它出来的时候很丑,但它确实有效
看看
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)