I'm attempting to combine my two array, but move only the object.
I have an array called parentArr that I am attempting to combine with growthArr. When using the .push() method it is returning parentArr as an array inside of growthArr. I am attempting to combine the objects from parentArr and not have it nested inside of an array.
Here is an example of what is happening:
let growthArr = [
{
name: 'online stores',
parent: 'high'
}, {
name: 'retail stores',
parent: 'low'
}, {
name: 'walk in',
parent: 'high'
}
]
let parentArr = [
{
id: 'low',
color: '#fafafa'
}, {
id: 'med-low',
color: '#B62721'
}, {
id: 'med',
color: '#FF5733'
}, {
id: 'med-high',
color: '#FF33FC'
}, {
id: 'high',
color: '#33FF64'
}
]
growthArr.push(parentArr)
console.log(growthArr)
To work around this, I've tried doing growthArr.push.apply(parentArr) but by doing this my parentArr does not show at all.
Here is my expected outcome, an array with both the objects in growthArr and parentArr
[
{
"name": "online stores",
"parent": "high"
},
{
"name": "retail stores",
"parent": "low"
},
{
"name": "walk in",
"parent": "high"
},
{
"id": "low",
"color": "#fafafa"
},
{
"id": "med-low",
"color": "#B62721"
},
{
"id": "med",
"color": "#FF5733"
},
{
"id": "med-high",
"color": "#FF33FC"
},
{
"id": "high",
"color": "#33FF64"
}
]
-
1Does this answer your question? How to merge two arrays in JavaScript and de-duplicate itemsuser796446– user7964462020年06月24日 17:45:17 +00:00Commented Jun 24, 2020 at 17:45
9 Answers 9
Use the ... syntax with .push()
growthArr.push(...parentArr)
This works because .push() takes any number of arguments, and pushes them all into the targeted array.
Note: This mutates the original array without creating a new one and overwriting it. This can be important if there are other references to the array that need to observe the mutation.
FYI, your attempt would have worked:
growthArr.push.apply(parentArr)
Except that .apply expects the first argument to be the this value (the targeted array). So you'd need this:
growthArr.push.apply(growthArr, parentArr)
Comments
concat is what you want to use.
let growthArr = [{
name: 'online stores',
parent: 'high'
}, {
name: 'retail stores',
parent: 'low'
}, {
name: 'walk in',
parent: 'high'
}]
let parentArr = [{
id: 'low',
color: '#fafafa'
}, {
id: 'med-low',
color: '#B62721'
}, {
id: 'med',
color: '#FF5733'
}, {
id: 'med-high',
color: '#FF33FC'
}, {
id: 'high',
color: '#33FF64'
}]
console.log(
growthArr.concat(parentArr)
)
1 Comment
Use ...array for the values of an array.
let growthArr = [{
name: 'online stores',
parent: 'high'
}, {
name: 'retail stores',
parent: 'low'
}, {
name: 'walk in',
parent: 'high'
}];
let parentArr = [{
id: 'low',
color: '#fafafa'
}, {
id: 'med-low',
color: '#B62721'
}, {
id: 'med',
color: '#FF5733'
}, {
id: 'med-high',
color: '#FF33FC'
}, {
id: 'high',
color: '#33FF64'
}];
growthArr = [...growthArr, ...parentArr];
console.log(growthArr);
1 Comment
Try this .concat. parentArr.concat(growthArr)
Comments
You have to use **concat** instead of push
let growthArr = [
{
name: 'online stores',
parent: 'high'
}, {
name: 'retail stores',
parent: 'low'
}, {
name: 'walk in',
parent: 'high'
}
]
let parentArr = [
{
id: 'low',
color: '#fafafa'
}, {
id: 'med-low',
color: '#B62721'
}, {
id: 'med',
color: '#FF5733'
}, {
id: 'med-high',
color: '#FF33FC'
}, {
id: 'high',
color: '#33FF64'
}
]
console.info(growthArr.concat(parentArr))
Comments
Use parentArr.concat(growthArr).
JS can be as simple as that!
Use array concat() method. So the syntax would be
parentArr.concat(growthArr);
2 Comments
All you need for this is the handy spread syntax
const
growthArr = getGrowthArr(),
parentArr = getParentArr();
growthArr.push(...parentArr);
console.log(growthArr);
function getGrowthArr() {
return [{
name: 'online stores',
parent: 'high'
}, {
name: 'retail stores',
parent: 'low'
}, {
name: 'walk in',
parent: 'high'
}];
}
function getParentArr() {
return [{
id: 'low',
color: '#fafafa'
}, {
id: 'med-low',
color: '#B62721'
}, {
id: 'med',
color: '#FF5733'
}, {
id: 'med-high',
color: '#FF33FC'
}, {
id: 'high',
color: '#33FF64'
}];
}
Comments
This can also be done with destructuring in es6 while creating a new array:
const combined = [...growthArr, ...parentArr];