-1

I can't seem to figure this out.

I have two arrays. One of the arrays contains all the IDs of the other array, plus more.

var arr1 = [1,2,3,4,5]
var arr2 = [3,5]

My first array contains a lot more information which my second array does not (many other keys). I need to find a way to select all the elements of the first array that are present in the second array and return them so that I have just the elements of arr2 but with all the additional data in arr1. How can I do this?

EDIT: I should make it clear that in the first array, I am looking for specific IDs that match the indexes of the second array. So the solutions here are really good but not quite what I'm after. Example:

[ 0: { id: 1, name: "fred" } ...]

I want to match the id with my second array, not the index. Hope this makes sense!

Matt Saunders
  • 4,073
  • 7
  • 33
  • 47
  • 1
    Easy with something like [Lodash `_.difference`](https://lodash.com/docs/#difference). – tadman Mar 26 '18 at 17:34
  • Thanks, I've not used Lodash before, but I might look into it. In the meantime do you know a pure JS solution to this? – Matt Saunders Mar 26 '18 at 17:36
  • 1
    Well with the code you're showing you could simply return arr2 – powerbuoy Mar 26 '18 at 17:36
  • Please review [ask] and update your question to include a [mcve] of what you've tried. Otherwise it's difficult to tell what exactly you're struggling with. – zzzzBov Mar 26 '18 at 17:36
  • Lodash is a pure JavaScript solution to this. If you like how they do it, snip out their function and use it. – tadman Mar 26 '18 at 17:37
  • But there are loads of new array functions you could use if arr1 is in fact an array of objects, find() comes to mind. – powerbuoy Mar 26 '18 at 17:37
  • This seems to be similar to question https://stackoverflow.com/questions/1885557/simplest-code-for-array-intersection-in-javascript – nizantz Mar 26 '18 at 17:40
  • Lodash and the suggested answer work in that they return the correct array elements but it does not return the additional values. I think what I need to do is actually construct a new array using a combination of arr1 and arr2. – Matt Saunders Mar 26 '18 at 17:48

2 Answers2

0

Use filter and includes of Array.protitype.

var arr1 = [1,2,3,4,5]
var arr2 = [3,5,7]

console.log(arr1.filter(x=>!arr2.includes(x)));

arr2.forEach(function(x){
  if(!arr1.includes(x)){
    arr1.push(x);
  }
})

console.log(arr1);
yajiv
  • 2,901
  • 2
  • 15
  • 25
  • 1
    ***I need to find a way to select all the elements of the first array that are present in the second array*** I think it should be reversed `console.log(arr1.filter(x=>arr2.includes(x)));` – Scott Marcus Mar 26 '18 at 17:38
  • @ScottMarcus as far as I understood **I have just the elements of arr2 but with all the additional data in arr1** means array 1 contains additional data which is not in array2 – yajiv Mar 26 '18 at 17:40
0

I implemented a set data structure few months back, this is the difference function

function difference (firstarr,secondarr) {

    let diffSet = [];

    for ( let i = 0; i < secondarr.length ; i++ ) {
        let hasValue = secondarr.includes(firstarr[i]);
        if ( ! hasValue ) {
            diffSet.push(secondarr[i]);
        }
    }

    for ( let i = 0; i < firstarr.length ; i++ ) {
        let hasValue = secondarr.includes(firstarr[i]);
            if ( ! hasValue ) {
                diffSet.push(firstarr[i]);
            }
    }

    return diffSet;
};

console.log(difference([1,2,3,4],[3,4]));
0.sh
  • 2,659
  • 16
  • 37