-2

How to save javascript data from a loop to an array?

for (i = 0; i < jsonData.Data.Positions.length; i++) {
     var h = jsonData.Data.Positions[i].Oid;
}
3

6 Answers 6

0

Insert the data in the array using push

var arr=[];
for (i = 0; i < jsonData.Data.Positions.length; i++) {
     var h = jsonData.Data.Positions[i].Oid;
     arr.push(h);
}

Sign up to request clarification or add additional context in comments.

Comments

0
var data = [];
for (i = 0; i < jsonData.Data.Positions.length; i++) {
     var h = jsonData.Data.Positions[i].Oid;
     data.push(h)
}

//OR
var data = jsonData.Data.Positions.map(item => item.Oid);

Comments

0

Your variable jsonData.Data.Positions is probably already an array.

Comments

0

Use .push() method to add values to array.

var h=[];
for (i = 0; i < jsonData.Data.Positions.length; i++) {
     h.push(jsonData.Data.Positions[i].Oid);
}
console.log(h);

Comments

0

You can do it within a loop:

var array = []
for (i = 0; i < jsonData.Data.Positions.length; i++) {
     array.push(jsonData.Data.Positions[i].Oid);
}

Or in a more functional-way:

var array = jsonData.Data.Positions.map(p => p.Oid)

Comments

0

map instead of for loop

var h = jsonData.Data.Positions.map(function (x) { return x.0id });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.