admin管理员组

文章数量:1334930

I have two arrays like below.

A = [{fruit: apple, number:4 }, {fruit: pear, number: 3}]
B = [{qual: good}, {qual: bad}]

And My goal is getting an array like below.

C = [{fruit: apple, number:4, qual: good }, {fruit: pear, number: 3, qual: bad}]

The length of A, B is same. I can make this using 'for loop'. But how can I make it using 'some array methods' like 'concat' or 'map'?

I have two arrays like below.

A = [{fruit: apple, number:4 }, {fruit: pear, number: 3}]
B = [{qual: good}, {qual: bad}]

And My goal is getting an array like below.

C = [{fruit: apple, number:4, qual: good }, {fruit: pear, number: 3, qual: bad}]

The length of A, B is same. I can make this using 'for loop'. But how can I make it using 'some array methods' like 'concat' or 'map'?

Share Improve this question edited Sep 24, 2019 at 20:24 Dharman 33.4k27 gold badges101 silver badges147 bronze badges asked Sep 24, 2019 at 4:43 DD DDDD DD 1,2382 gold badges14 silver badges42 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 15

You can use map and spread syntax

let A = [{fruit: 'apple', number:4 }, {fruit: 'pear', number: 3}]
let B = [{qual: 'good'}, {qual: 'bad'}]

let C = A.map((value, index) => ({ ...value,  ...B[index] }))

console.log(C)

Index is used to access respective value from second array, and merged into a single object using spread syntax

Since you have not provided the condition to merge, I am assuming it is index based, mean first object from second array will go to first object in first array and so on. If that is the case you can use map which will create a new array. In map callback return a object with required keys and values

let a = [{
  fruit: 'apple',
  number: 4
}, {
  fruit: 'pear',
  number: 3
}]
let b = [{
  qual: 'good'
}, {
  qual: 'bad'
}]

let newArray = a.map((item, index) => {

  return {
    fruit: item.fruit,
    number: item.number,
    qual: b[index].qual
  }
})

console.log(newArray)

Try this:

Using for loop:

let array1 = [{ fruit: "apple", number: 4 }, { fruit: "pear", number: 3 }]
let array2 = [{ qual: "good" }, { qual: "bad" }]
for (let i = 0; i < array1.length; i++) {
  array1[i].qual = array2[i].qual;
}
console.log(array1)
Using map:

let array1 = [{fruit: 'apple', number:4 }, {fruit: 'pear', number: 3}]
let array2 = [{qual: 'good'}, {qual: 'bad'}]

let C = array1.map((value, index) => {
  return {
    fruit: value.fruit,
    number: value.number,
    qual: array2[index].qual
  }
})
console.log(C)

this seems to be slightly faster than the top answer but it's more code and top answer is much more straightforward.

const a = [{ fruit: "apple", number: 4 }, { fruit: "pear", number: 3 }];
const b = [{ qual: "good" }, { qual: "bad" }];
const c = [];

a.forEach((fruit, index) => {
    const newFruit = { ...fruit, ...b[index]}
    c.push(newFruit)
});

console.log(c);

or you can use use the spread operator directly let A = [{fruit: 'apple', number:4 }, {fruit: 'pear', number: 3}] let B = [{qual: 'good'}, {qual: 'bad'}]

let C = [...A, ...B]

console.log(C);

you can use array.concat method.

<!DOCTYPE html>
<html>
<body>

<p>Click the button to join two arrays.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
  var hege = ["Cecilie", "Lone"];
  var stale = ["Emil", "Tobias", "Linus"];
  var children = hege.concat(stale); 
  document.getElementById("demo").innerHTML = children;
}
</script>

</body>
</html>

本文标签: javascriptHow to merge two arrays in JSStack Overflow