无法在 JSON 对象中添加新的键值对 请有人帮助我
not able to add new key value pair in the JSON object kindly some one help me out
我正在尝试在下面的 JSON 对象中添加键值对,但是添加的方式不对,有什么办法可以为每个对象添加一个新的键值对,那个将帮助我完成要求。
JSON如下:
var object =[
{
"asdf":"",
"fdrtf":"869966",
"hdhfhfh":"utytut",
"Cat":"A"
},
{
"hjhj":"",
"hfhfhf":"h",
"hhfh":"hfjfhdj",
"cat":"B"
},
{
"hjhj":"",
"hfhfhf":"h",
"hhfh":"hfjfhdj",
"cat":"B"
}
]
code i am using to add the new key value pair :
for (let i = 0; i < object.length; i++) {
object[i]['ABC'] = [i];
}
实际预计应该是:
var object =[
{
"asdf":"",
"fdrtf":"869966",
"hdhfhfh":"utytut",
"Cat":"A",
"ABC": "0"
},
{
"hjhj":"",
"hfhfhf":"h",
"hhfh":"hfjfhdj",
"cat":"B",
"ABC": "1"
},
{
"hjhj":"",
"hfhfhf":"h",
"hhfh":"hfjfhdj",
"cat":"B"
"ABC": "2"
}
]
我得到的最终值如下:
["0":{
"hjhj":"",
"hfhfhf":"h",
"hhfh":"hfjfhdj",
"cat":"B",
},
"1":{
"asdf":"",
"fdrtf":"869966",
"hdhfhfh":"utytut",
"Cat":"A"
},
"2":{
"hjhj":"",
"hfhfhf":"h",
"hhfh":"hfjfhdj",
"cat":"B"
}
]
好心人帮我弄出期望值,真的很需要
您粘贴的代码看起来没问题。但是您正在将数组分配给 ABC
属性.
去掉数组,直接赋值。
for (let i = 0; i < object.length; i++) {
object[i]['ABC'] = i
}
工作示例:
var object = [
{
"asdf": "",
"fdrtf": "869966",
"hdhfhfh": "utytut",
"Cat": "A"
},
{
"hjhj": "",
"hfhfhf": "h",
"hhfh": "hfjfhdj",
"cat": "B"
},
{
"hjhj": "",
"hfhfhf": "h",
"hhfh": "hfjfhdj",
"cat": "B"
}
]
for (let i = 0; i < object.length; i++) {
object[i]['ABC'] = i
}
console.log(object);
//Use JSON.stringify if you need Json string
console.log(JSON.stringify(object));
使用 ES6 map
和扩展运算符
var object = [
{
"asdf": "",
"fdrtf": "869966",
"hdhfhfh": "utytut",
"Cat": "A"
},
{
"hjhj": "",
"hfhfhf": "h",
"hhfh": "hfjfhdj",
"cat": "B"
},
{
"hjhj": "",
"hfhfhf": "h",
"hhfh": "hfjfhdj",
"cat": "B"
}
]
let updatedObject = object.map( (item, index) => ({...item, 'ABC': index}));
console.log(updatedObject);
//Use JSON.stringify if you need Json string
console.log(JSON.stringify(updatedObject));
我正在尝试在下面的 JSON 对象中添加键值对,但是添加的方式不对,有什么办法可以为每个对象添加一个新的键值对,那个将帮助我完成要求。
JSON如下:
var object =[
{
"asdf":"",
"fdrtf":"869966",
"hdhfhfh":"utytut",
"Cat":"A"
},
{
"hjhj":"",
"hfhfhf":"h",
"hhfh":"hfjfhdj",
"cat":"B"
},
{
"hjhj":"",
"hfhfhf":"h",
"hhfh":"hfjfhdj",
"cat":"B"
}
]
code i am using to add the new key value pair :
for (let i = 0; i < object.length; i++) {
object[i]['ABC'] = [i];
}
实际预计应该是:
var object =[
{
"asdf":"",
"fdrtf":"869966",
"hdhfhfh":"utytut",
"Cat":"A",
"ABC": "0"
},
{
"hjhj":"",
"hfhfhf":"h",
"hhfh":"hfjfhdj",
"cat":"B",
"ABC": "1"
},
{
"hjhj":"",
"hfhfhf":"h",
"hhfh":"hfjfhdj",
"cat":"B"
"ABC": "2"
}
]
我得到的最终值如下:
["0":{
"hjhj":"",
"hfhfhf":"h",
"hhfh":"hfjfhdj",
"cat":"B",
},
"1":{
"asdf":"",
"fdrtf":"869966",
"hdhfhfh":"utytut",
"Cat":"A"
},
"2":{
"hjhj":"",
"hfhfhf":"h",
"hhfh":"hfjfhdj",
"cat":"B"
}
]
好心人帮我弄出期望值,真的很需要
您粘贴的代码看起来没问题。但是您正在将数组分配给 ABC
属性.
去掉数组,直接赋值。
for (let i = 0; i < object.length; i++) {
object[i]['ABC'] = i
}
工作示例:
var object = [
{
"asdf": "",
"fdrtf": "869966",
"hdhfhfh": "utytut",
"Cat": "A"
},
{
"hjhj": "",
"hfhfhf": "h",
"hhfh": "hfjfhdj",
"cat": "B"
},
{
"hjhj": "",
"hfhfhf": "h",
"hhfh": "hfjfhdj",
"cat": "B"
}
]
for (let i = 0; i < object.length; i++) {
object[i]['ABC'] = i
}
console.log(object);
//Use JSON.stringify if you need Json string
console.log(JSON.stringify(object));
使用 ES6 map
和扩展运算符
var object = [
{
"asdf": "",
"fdrtf": "869966",
"hdhfhfh": "utytut",
"Cat": "A"
},
{
"hjhj": "",
"hfhfhf": "h",
"hhfh": "hfjfhdj",
"cat": "B"
},
{
"hjhj": "",
"hfhfhf": "h",
"hhfh": "hfjfhdj",
"cat": "B"
}
]
let updatedObject = object.map( (item, index) => ({...item, 'ABC': index}));
console.log(updatedObject);
//Use JSON.stringify if you need Json string
console.log(JSON.stringify(updatedObject));