0

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
3
  • 1
    What is your expected result? Commented Aug 13, 2021 at 13:01
  • I am adding the array of an objects into object ( which exist already in Mongodb), I am accesing the db object, but not able to insert an array in that object Commented Aug 13, 2021 at 13:03
  • 1
    Object.assign(yourObject, { myArray }) Commented Aug 13, 2021 at 13:17

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.