0

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'?

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

2 Comments

How is index referring to respective value from the second array?
@Bhawan array are list like objects, and can be accessed using index, Array
2

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)

Comments

2

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)

Comments

1

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);

Comments

0

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);

Comments

0

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>

Comments

Your Answer

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.