1

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
1
  • 1
    Where does the value of id come from in this loop? Commented Jun 12, 2015 at 17:02

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

Oh, that great. I have it working well now as you recomended:
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);
@PetarKirilovGeorgiev I'm glad I could help. If you liked my answer, please accept it by clicking the checkmark to the left of it.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.