Home > Mobile >  Javascript: How to extend an array with a dictionary with { key: array values} without logging [Obje
Javascript: How to extend an array with a dictionary with { key: array values} without logging [Obje

Time:04-17

Say I have a dictionary

h =  { toys: [ { name: 'Toy 1', price: 900 } ] }

and an array

arr = []

How can extend arr with h using push or something else to have

arr.push(h) gives [ { toys: [ [Object] ] } ]

Desired result is:

 [ { toys: [  { name: 'Toy 1', price: 900 } ]  } ]

CodePudding user response:

arr.push(h) gives [ { toys: [ [Object] ] } ]

This is because console.log somewhere does not show the deep levels of nested objects. You just need to use console.dir with the depth option. Check this out:

h =  { toys: [{ name: 'Toy 1', price: 900 }] };
arr = [];
arr.push(h);
console.dir(arr, { depth: null });

CodePudding user response:

Like that:

const h =  { toys: [ { name: 'Toy 1', price: 900 } ] };
const arr = [h];
console.log(arr);

  • Related