Home > Mobile >  Javascript array object [closed]
Javascript array object [closed]

Time:09-22

How to store an array to a object like

const obj = [{}];
const arr = ['Peter','Tony','Rahul'];

const obj = [{"name":'Peter', "age":19},{"name":'Tony', "age":20},{"name":'Rahul',"age":25}];

CodePudding user response:

Try this. It may help you

const obj = []; const arr = ['Peter','Tony','Rahul'];
arr.forEach(name => obj.push({name}))
console.log(obj);

CodePudding user response:

const obj = [];
const arr = ['Peter','Tony','Rahul'];
const ages= [19,20,25];
for (a in arr){
    obj.push({name:arr[a],age:ages[a]});
}
console.log(obj);
  • Related