0

I know this is silly question but I unable to do it. I declare variable arr=[]; and i'm pushing object value dynamically. But no luck. I want array in following format

arr[
   {name:"abc"},
   {name:"pqr"}
];

But I unable to get in above format. My code is:

var arr= [];
for (let i = 1; i < 100; i++) {
  let lg= {
  name:list[i]
};
arr.push(lg);
}

Where i'm doing wrong. Please help me.

3
  • What is list and log?? Commented Apr 18, 2017 at 11:57
  • @developer033, Thank you for response. list is array and its contains only string. Commented Apr 18, 2017 at 11:59
  • @Julien, Thank you for response. By mistake I wrote log.push() at the time of writing question. Commented Apr 18, 2017 at 12:02

2 Answers 2

8

assume you have list array,

let arr = [];
for (let i = 0; i < list.length; i++) { //be careful about array length, and index maybe start from zero
  let lg = {
    name: list[i]
  };
  arr.push(lg); // not log
}

code demo here: http://jsbin.com/xazihekuti/edit?js,console

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

1 Comment

@AnilJagtap, if two answers are exactly the same, it's usually polite to mark the one that was posted first as the correct answer ;)
2
 let lg : {name: string};   
 for (let i = 0; i < list.length; i++) { 
       lg = {
        name: list[i]
      };
      arr.push(lg);
 }

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.