1

How can i append items to an object programmatically without having to hard-code the object key:

let myObject = {};
let list = ['seeds','leaves','branches'];
let list2 = ['100','1 million','22'];

// list2.forEach((item,index) => {
//  console.log(index);
// myObject.list[index] = item;
// });

for(i=0; i<list.length; i++){
  //console.log(list[i])
  myObject.list[i] = list2[i];
}
console.log(myObject);

I want it to have an object like this

{
seeds:'100',
leaves: '1 million',
branchces: '22'
}

when i run this however i get:

error: Uncaught TypeError: Cannot set property '0' of undefined

xaander1
  • 1,064
  • 2
  • 12
  • 40

0 Answers0