I am trying to insert an array of objects into an object, so far I search but I was not able to find such what exactly I am looking for.
myArray = [ { name: 'TS', width: '11', height: '2', order: 3 },
{ name: 'TS', width: '11',height: '2',order: 3 } ]
is there any function available which I can use to add/push the above array into an object
asked Aug 13, 2021 at 12:59
robinHood013
2191 gold badge2 silver badges15 bronze badges
1 Answer 1
You can use bracket (myObject['arrayName']), or dot notation (myObject.arrayName) to assign the array to an object property, for example:
const myObject = { id: 42, name: 'myObject' };
const myArray = [ { name: 'TS', width: '11', height: '2', order: 3 },
{ name: 'TS', width: '11',height: '2',order: 3 } ]
const arrayName = 'myArray';
myObject[arrayName] = myArray;
console.log('myObject:', myObject);
answered Aug 13, 2021 at 13:04
Terry Lennox
30.9k5 gold badges37 silver badges45 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js
Object.assign(yourObject, { myArray })