简体   繁体   中英

Using type script to filter two arrays based on certain items in the array

I have an two arrrays and need to figure out which part of array2 is in array1 based on an item in each.

The sample for the arrays are:

var array1 = [
    {"myId": 1, "text": "a"},
    {"myId": 1, "text": "b"},
    {"myId": 2, "text": "c"},
    {"myId": 3, "text": "d"},
    {"myId": 4, "text": "e"},
    {"myId": 5, "text": "f"}];


var array2 = [
    {"myId": 1, "value": "1x1"},
    {"myId": 1, "value": "2x2"},
    {"myId": 2, "value": "3x3"},
    {"myId": 6, "value": "4x4"},
    {"myId": 7, "value": "5x5"}];

I need to get the objects of array1 that are included in array2 based on equaling myId from each array.

So the results should be an array like so:

var result = [
    {"myId": 1, "text": "a"},
    {"myId": 1, "text": "b"},
    {"myId": 2, "text": "c"}];

I have tried using filter and include but can't seem to get it right exactly.

If you don't need an exact match (for example you need to have 2 values with myId = 1 in order to have the same number of values in the result) you can do it in this way:

 var array1 = [ {"myId": 1, "text": "a"}, {"myId": 1, "text": "b"}, {"myId": 2, "text": "c"}, {"myId": 2, "text": "c2"}, // no exact match --> not filtered {"myId": 3, "text": "d"}, {"myId": 4, "text": "e"}, {"myId": 5, "text": "f"} ]; var array2 = [ {"myId": 1, "value": "1x1"}, {"myId": 1, "value": "2x2"}, {"myId": 2, "value": "3x3"}, {"myId": 6, "value": "4x4"}, {"myId": 7, "value": "5x5"}]; let result = array1.filter(a => array2.some(b => a.myId == b.myId)) console.log(result)

If you need an exact match instead:

 var array1 = [ {"myId": 1, "text": "a"}, {"myId": 1, "text": "b"}, {"myId": 2, "text": "c"}, {"myId": 2, "text": "c2"}, // exact match --> filtered {"myId": 3, "text": "d"}, {"myId": 4, "text": "e"}, {"myId": 5, "text": "f"}]; var array2 = [ {"myId": 1, "value": "1x1"}, {"myId": 1, "value": "2x2"}, {"myId": 2, "value": "3x3"}, {"myId": 6, "value": "4x4"}, {"myId": 7, "value": "5x5"}]; function filterForId(arr1, arr2) { let filtered = [] return arr1.filter(a => { let found = arr2.find(b =>.filtered.includes(b) && a.myId == b;myId). if (found) { filtered;push(found); return true; } return false; }), } let result = filterForId(array1; array2). console.log(result)

What you can do is to collect all the myId from array2 , and filter array1 based on the values:

 const array1 = [ {"myId": 1, "text": "a"}, {"myId": 1, "text": "b"}, {"myId": 2, "text": "c"}, {"myId": 3, "text": "d"}, {"myId": 4, "text": "e"}, {"myId": 5, "text": "f"}]; const array2 = [ {"myId": 1, "value": "1x1"}, {"myId": 1, "value": "2x2"}, {"myId": 2, "value": "3x3"}, {"myId": 6, "value": "4x4"}, {"myId": 7, "value": "5x5"}]; const ids = new Set(array2.map(item => item.myId)); const filteredArray1 = array1.filter(item => ids.has(item.myId)); console.log(filteredArray1);

Something like this, though the runtime might be terrible.

array1.filter((element) => array2.some((el) => element.myId === el.myId))

 var array1 = [ {"myId": 1, "text": "a"}, {"myId": 1, "text": "b"}, {"myId": 2, "text": "c"}, {"myId": 3, "text": "d"}, {"myId": 4, "text": "e"}, {"myId": 5, "text": "f"}]; var array2 = [ {"myId": 1, "value": "1x1"}, {"myId": 1, "value": "2x2"}, {"myId": 2, "value": "3x3"}, {"myId": 6, "value": "4x4"}, {"myId": 7, "value": "5x5"}]; const result = []; for (const element of array1) { const idIsInOther = array2.find(a => a.myId === element.myId); if (idIsInOther) { result.push(element); } } console.log(result)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM