I am trying to add key value pair in the below JSON object, but it is getting added in the wrong way, is there any way i can add to each object a new key value pair, that will help me to complete the req.
JSON as follow :
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];
}
Actually expected should be:
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"
}
]
the final value i am getting as below:
["0":{
"hjhj":"",
"hfhfhf":"h",
"hhfh":"hfjfhdj",
"cat":"B",
},
"1":{
"asdf":"",
"fdrtf":"869966",
"hdhfhfh":"utytut",
"Cat":"A"
},
"2":{
"hjhj":"",
"hfhfhf":"h",
"hhfh":"hfjfhdj",
"cat":"B"
}
]
kindly someone help me out to achieve this