0

I have an array and an object and I would like to combine them by nesting the array within the object using JavaScript. I do not want to use lodash, jQuery, or any other library because my project is fairly small and I cannot add that overhead.

This is my current object and array:

var myObject = {
 "key1": "value 1",
 "key2": "value 2"
}
var myArray = [
 { "id": 1, "name": "name 1"},
 { "id": 2, "name": "name 2"}
]

And this is how I would like it to be structured:

var myObject = {
 "key1": "value 1",
 "key2": "value 2",
 "myArray": [
 { "id": 1, "name": "name 1"},
 { "id": 2, "name": "name 2"}
 ]
}

I've tried using the JavaScript push() method, but that didn't work for me. Any guidance would be greatly appreciated. Thanks!

asked Jun 8, 2018 at 18:31
1
  • push it is for Arrays Commented Jun 8, 2018 at 18:32

5 Answers 5

2

Just make an assignment to the object of your array.

myObject.myArray = myArray;

Be aware that array's are assigned by reference by default. So changing either will change both variables.

answered Jun 8, 2018 at 18:33
Sign up to request clarification or add additional context in comments.

2 Comments

What exactly do you mean by "array's are assigned by reference by default"? I appreciate the added explanation and would like to use this as a learning opportunity.
@spookybot so, in JS objects (which Array is actually a subtype of) are assigned by reference and mutable. Which means if I say var a = {}; and var b = a; both a and b contain the same object. so a.foo = "bar"; console.log(b.foo);//outputs "bar" this is in contrast to scalar types (numbers, strings, boolean) which are assigned by value. If you want to be able to change myArray and myObject.myArray independently of each other, you need to make a copy of the array instead. Frequently done via myObject.myArray = myArray.slice(). Hope that helps a little bit.
1

myObject.myArray = myArray

var myObject = {
 "key1": "value 1",
 "key2": "value 2"
}
var myArray = [
 { "id": 1, "name": "name 1"},
 { "id": 2, "name": "name 2"}
]
myObject.myArray = myArray
console.log(myObject)

answered Jun 8, 2018 at 18:32

Comments

1
myObject.myArray = myArray

Keep it simple.

answered Jun 8, 2018 at 18:33

Comments

0

 var myObject = {
 "key1": "value 1",
 "key2": "value 2"
 }
 
 var myArray = [
 { "id": 1, "name": "name 1"},
 { "id": 2, "name": "name 2"}
 ];
 
 myObject['myArray'] = myArray;
 
 console.log(myObject);

answered Jun 8, 2018 at 18:32

Comments

0

Using the newest rest and spread syntax, you could also achieve it like this.

let myObject = {...myObject, myArray}
answered Jun 8, 2018 at 18:35

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.