1

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
}
}
1
  • have you tried indexed interface in typescript ? Commented Apr 18, 2018 at 5:11

4 Answers 4

3

Its supposed to be like this...

let someData : any [] = [];

someData.push({
"a" : "a1",
"b" : b1"
})

someData[0]["c"] = "c1";
someData[0]["d"] = "d1";

So when you log the values of someData ... it will show

console.log(someData); //[{"a":"a1","b" : "b1", "c":"c1", "d":"d1"}]

for looping through values...

let valuesToPut = [["c","c1"],["d","d1"]];

 for(let i = 0; i < valuesToPut.length; i++){
    someData[0][valuesToPut[i][0]] = valuesToPut[i][1]
 }
Sign up to request clarification or add additional context in comments.

2 Comments

This is partially working.. the case it does not work is when you try putting a for loop and use the values like below for(var i=0; i<3; i++){ someData[i]["c"] = "c1"; } in this case it is not working.
Sorry, i was mistaken in my for loop, now it is working fine.
1

is little confusion between Object and Array, here you try to add some item to the Object who are store on index 0 of your array.

let try following code :

let someData : any [] = [];
// Create first index of array and create on it your Object.
someData.push({
    "a" : "a1",
    "b" : b1"
});

// Override first index of array by merge of previous data + new one.
someData[0] = Object.assign(someData[0], {
    "c1" : "c1",
    "d1" : "d1"
});

Object assign documentation

Another way to do this: Object.defineProperties()

Comments

1

As you mensioned, it is an array json format.

So, if you access some element in the array, you should indicate the array index.

ex:

let tempArray = [ 
{
"a" : "a1",
"b" : "b1"
}
]  

=> tempArray[0] has this value.

{
"a" : "a1",
"b" : "b1"
}

So, if you add some additional values to the tempArray[0], you should access the element like below :

tempArray[0]['c'] = "c1";
tempArray[0]['d'] = "d1";

Comments

1

//Let's start with what you're doing
let someData = [];

someData.push({
"a" : "a1",
"b" : "b1"
});
// pushes a collection of 2 key value pairs in as the first entry to someData
someData.push({
"c" : "c1",
"d" : "d1"
});
// pushes a collection of 2 key value pairs in as the second entry to someData
console.log(someData);

// what you might want to do:
someData = [];

someData.push({
"a" : "a1",
"b" : "b1"
});
// pushes a collection of 2 key value pairs in as the first entry to someData
someData[0].c = "c1";
//Sets a value to the key c in the first element of some data
someData[0].d = "d1";
//Sets a value to the key d in the first element of some data
console.log(someData)

// What you probably want to do.
someData = {};
for(let i of [1,2,3,4]){
  someData[String.fromCharCode(i+96)] = String.fromCharCode(i+96)+"1";
}
console.log(someData)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.