Javascript filter by data from another

I have an array object:

[
    { id:1, name: 'Pedro'},
    { id:2, name: 'Miko'},
    { id:3, name: 'Bear'},
    { id:4, name: 'Teddy'},
    { id:5, name: 'Mouse'}
]

and have an array with identifiers [1,3,5]

How to filter an array object to leave records with only the identifier from the second? Thanks!

+7
source share
7 answers

If supported, you can use it with to get elements: Array.includes() Array.filter()

const array = [
  { id: 1, name: 'Pedro'},
  { id: 2, name: 'Miko'},
  { id: 3, name: 'Bear'},
  { id: 4, name: 'Teddy'},
  { id: 5, name: 'Mouse'}
];

const filterArray = [1,3,5];

const result = array.filter(({ id }) => filterArray.includes(id));

console.log(result);
Run codeHide result

If not supported, you can use : Array.indexOf() Array.indexOf()

var array = [
  { id: 1, name: 'Pedro'},
  { id: 2, name: 'Miko'},
  { id: 3, name: 'Bear'},
  { id: 4, name: 'Teddy'},
  { id: 5, name: 'Mouse'}
];

var filterArray = [1,3,5];

var result = array.filter(function(item) {
    return filterArray.indexOf(item.id) !== -1;
});

console.log(result);
Run codeHide result
+11
source

Perhaps take Array.prototype.reducein conjunction with Array.prototype.some. This preserves the order of the given array need.

var data = [
        { id: 3, name: 'Bear' },
        { id: 4, name: 'Teddy' },
        { id: 5, name: 'Mouse' },
        { id: 1, name: 'Pedro' },
        { id: 2, name: 'Miko' },
    ],
    need = [1, 3, 5],
    filtered = need.reduce(function (r, a) {
        data.some(function (el) {
            return a === el.id && r.push(el);
        });
        return r;
    }, []);

document.write('<pre>' + JSON.stringify(filtered, 0, 4) + '</pre>');
Run codeHide result

data, Array.prototype.filter:

var data = [
        { id: 3, name: 'Bear' },
        { id: 4, name: 'Teddy' },
        { id: 5, name: 'Mouse' },
        { id: 1, name: 'Pedro' },
        { id: 2, name: 'Miko' },
    ],
    need = [1, 3, 5],
    filtered = data.filter(function (a) {
        return ~need.indexOf(a.id);
    });

document.write('<pre>' + JSON.stringify(filtered, 0, 4) + '</pre>');
Hide result
+2

, , ( indexOf).

O (n ^ 2), , , , . .

:

function filterFast(data, ids) {
    var index = ids.reduce(function(a,b) {a[b] = 1; return a;}, {});
    return data.filter(function(item) {
        return index[item.id] === 1;
    });
}

.

+1

for hasOwnProperty [1,3,5] ( ). ( for-, ). (, ), , .

var c = 0;
for(var i =0; i< objects.length; i++){
  for(var v =0; v< list.length; v++)
     if(objects[i].hasOwnProperty(list[v])){ 
       delete objects[i]; c++; break; 
     }
  if(c===list.length) break;
}

array.splice( i, 1 );, .

0

:

var data = [
    { id:1, name: 'Pedro'},
    { id:2, name: 'Miko'},
    { id:3, name: 'Bear'},
    { id:4, name: 'Teddy'},
    { id:5, name: 'Mouse'}
];

var ids = [1, 3, 5];

var filteredData = filterData(data, 'id', ids[1]);

function filterData(data, prop, values) {
    return data.filter(function(item) {
        return ~values.indexOf(item[prop]); // ~ returns 0 if indexOf returns -1
    });
}

JSFiddle.

0

Or, if you are using jQuery, another option might be:

var arr1 = [1, 3, 5],
    arr2 = [{ id: 1, name: 'Pedro' },
    { id: 2, name: 'Miko' },
    { id: 3, name: 'Bear' },
    { id: 4, name: 'Teddy' },
    { id: 5, name: 'Mouse' }],
    filtered = $.grep(arr2, function (item) {
    if (arr1.indexOf(item.id) > -1) {
        return true;
    }
});
0
source

Use filterand indexOfwill do the trick:

var filteredArray = dataArray.filter(function(obj) {
  return idsArray.indexOf(obj.id) > -1;
});

However, it indexOfhas linear performance, and it will be called many times.

In ES6, you can instead use a set whose call hashas sublinear performance (average):

var idsSet = new Set(idsArray),
    filteredArray = dataArray.filter(obj => idsSet.has(obj.id));

Assuming toStringyour identifiers method is injective, you can achieve something similar in ES5:

var idsHash = Object.create(null);
idsArray.forEach(function(id) {
  idsHash[id] = true;
});
var filteredArray = dataArray.filter(function(obj) {
  return idsHash[obj.id];
});
-1
source

Source: https://habr.com/ru/post/1605739/


All Articles