0

edited:

var arr1 = [{id=1}, {id=3]
var arr2 = [{id=1, text=1}, {id=2, text=2}, {id=4, text=4}]

I'm trying to merge the arr1 in the arr2 except when the key id value is the same.

This should be the output:

var arr3 = [{id=1, text=1}, {id=2, text=2}, {id=3, text=3}, {id=4, text=4}]

To do that I'm trying to use if arr1.indexOf(arr2) !== 0 push values in arr3; but dosn't work.

var arr1 = [{id=1, text=1}, {id=3, text=3}]
var arr2 = [{id=1, text=1}, {id=2, text=2}, {id=4, text=4}]

var arr3 = [];

    for (var i = 0; i < arr2.length; i++) {

            if (arr1.indexOf(arr2[i].id) !== 0) {
                arr3.push({
                  id: arr2[i].id, 
                  stars: arr2[i].text 
                  });
            }
        }

Any help?

2
  • In your script, stars of stars: arr2[i].text is text? Commented May 6, 2021 at 12:14
  • Possible duplicate, i would look for an answer to your question here: stackoverflow.com/questions/46849286/… Also this is not valid JS: var arr1 = [{id=1}, {id=3] -> var arr1 = [{id:1}, {id:3}] Commented May 6, 2021 at 12:17

1 Answer 1

1

Modification points:

  • In your script, arr1.indexOf(arr2[i].id) is always -1. Because each element of arr1 is an object. In this case, indexOf cannot be directly used. So in this case, arr2 is copied to arr3. And, the values of arr1 are not used. I think that this might be the reason of your issue.
  • In your question, you say that you want var arr3 = [{id=1, text=1}, {id=2, text=2}, {id=3, text=3}, {id=4, text=4}]. But in your script, it seems that the key is changed from text to stars like stars: arr2[i].text.

When above points are reflected to your script, it becomes as follows.

Modified script 1:

In this modified script, the values of arr2 is merged to arr1.

var arr1 = [{ id: 1, text: 1 }, { id: 3, text: 3 }];
var arr2 = [{ id: 1, text: 1 }, { id: 2, text: 2 }, { id: 4, text: 4 }];

for (var i = 0; i < arr2.length; i++) {
  if (!arr1.some(({id}) => id == arr2[i].id)) {
    arr1.push({id: arr2[i].id, text: arr2[i].text});
  }
}
arr1.sort((a, b) => a.id > b.id ? 1 : -1);
console.log(arr1)

  • If arr1.sort((a, b) => a.id > b.id ? 1 : -1); is removed, the result array is not sorted as the ID.

Modified script 2:

As other sample script, how about the following script? In this modified script, the values of arr2 and arr2 are merged and removed the duplicated values.

var arr1 = [{ id: 1, text: 1 }, { id: 3, text: 3 }];
var arr2 = [{ id: 1, text: 1 }, { id: 2, text: 2 }, { id: 4, text: 4 }];

var arr3 = [...arr1, ...arr2].reduce((ar, e) => {
  if (!ar.some(({id}) => id == e.id)) ar.push(e);
  return ar;
}, []).sort((a, b) => a.id > b.id ? 1 : -1);
console.log(arr3)

References:

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

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.