I have a case like, I want to add elements into a JSON array in TypeScript like below
[
{
"a" : "a1",
"b" : "b1"
}
]
Now I want to add values to the above object without creating new block, for example, I want to add key, value pair as "C": "c1" and "d": "d1". After doing this, my JSON array must look like below
[
{
"a" : "a1",
"b" : "b1"
"c" : "c1",
"d" : "d1"
}
]
What I tried:
let someData : any [] = [];
someData.push({
"a" : "a1",
"b" : b1"
})
someData.push({
"c" : "c1",
"d" : d1"
})
But it is creating two records like below, but this is wrong
[
{
"a" : "a1",
"b" : "b1"
}
{
"c" : "c1",
"d" : "d1"
}
]
as well I tried using unshift as below
someData.unshift({
"c" : "c1",
"d" : d1"
})
this is returning result object as
[
{
"a" : "a1",
"b" : "b1"
}
{
"c" : "c1",
"d" : "d1"
}
]
Is there any way to do?
For example,
for(int i =0; i<3; i++){
someData.push({
i : i+1
})
But the above block of code is creating wrong array structure, but inturn I want as below
{
0 :1,
1:2,
2:3
}
}