I would like to create on JavaScript an object of one array (line_items_object) that contains objects with a for loop like this structure:
var data = {
line_items_object: [
{
id: 8111696
},
{
id: 8111688
}
]
};
How can I do that?
AlliterativeAlice
12.7k9 gold badges56 silver badges73 bronze badges
asked Jun 12, 2015 at 17:00
Petar Kirilov Georgiev
556 bronze badges
1 Answer 1
This will create an array of 100 objects with id values from 1 to 100. If id needs to be calculated or pulled from somewhere, let me know and I'll update the answer.
var data = { line_items_object: [] };
for (var i = 1; i <= 100; i++) data.line_items_object.push({id: i});
answered Jun 12, 2015 at 17:04
AlliterativeAlice
12.7k9 gold badges56 silver badges73 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Petar Kirilov Georgiev
Oh, that great. I have it working well now as you recomended:
Petar Kirilov Georgiev
var data = { line_items_object: [] }; for (var i = line_items.length-1; i >= 0; i--) data.line_items_object.push({id: line_items[i]['id']}); console.log(data);
AlliterativeAlice
@PetarKirilovGeorgiev I'm glad I could help. If you liked my answer, please accept it by clicking the checkmark to the left of it.
lang-js
idcome from in this loop?