How to filter array when object key value is in array

前端 未结 6 812
迷失自我
迷失自我 2020-11-28 08:16

I have an array model as below:

records:[{
    \"empid\":1,
    \"fname\": \"X\",
    \"lname\": \"Y\"
},
{
    \"empid\":2,
    \"fname\": \"A\",
    \"lnam         


        
相关标签:
6条回答
  • 2020-11-28 08:29

    You can use Array#filter function and additional array for storing sorted values;

    var recordsSorted = []
    
    ids.forEach(function(e) {
        recordsSorted.push(records.filter(function(o) {
            return o.empid === e;
        }));
    });
    
    console.log(recordsSorted);
    

    Result:

    [ [ { empid: 1, fname: 'X', lname: 'Y' } ],
      [ { empid: 4, fname: 'C', lname: 'Y' } ],
      [ { empid: 5, fname: 'C', lname: 'Y' } ] ]
    
    0 讨论(0)
  • 2020-11-28 08:30

    You can do it with Array.prototype.filter(),

    var data = { records : [{ "empid": 1, "fname": "X", "lname": "Y" }, { "empid": 2, "fname": "A", "lname": "Y" }, { "empid": 3, "fname": "B", "lname": "Y" }, { "empid": 4, "fname": "C", "lname": "Y" }, { "empid": 5, "fname": "C", "lname": "Y" }] }
    var empIds = [1,4,5]
    var filteredArray = data.records.filter(function(itm){
      return empIds.indexOf(itm.empid) > -1;
    });
    
    filteredArray = { records : filteredArray };
    

    If​ the ​callBack​ returns a ​true​ value, then the ​itm​ passed to that particular callBack will be filtered out. You can read more about it here.​​​​​​

    0 讨论(0)
  • 2020-11-28 08:30

    In 2019 using ES6:

    const ids = [1, 4, 5],
      data = {
        records: [{
          "empid": 1,
          "fname": "X",
          "lname": "Y"
        }, {
          "empid": 2,
          "fname": "A",
          "lname": "Y"
        }, {
          "empid": 3,
          "fname": "B",
          "lname": "Y"
        }, {
          "empid": 4,
          "fname": "C",
          "lname": "Y"
        }, {
          "empid": 5,
          "fname": "C",
          "lname": "Y"
        }]
      };
    
    
    data.records = data.records.filter( i => ids.includes( i.empid ) );
    
    console.info( data );

    0 讨论(0)
  • 2020-11-28 08:36

    Fastest way (will take extra memory):

    var empid=[1,4,5]
    var records = [{ "empid": 1, "fname": "X", "lname": "Y" }, { "empid": 2, "fname": "A", "lname": "Y" }, { "empid": 3, "fname": "B", "lname": "Y" }, { "empid": 4, "fname": "C", "lname": "Y" }, { "empid": 5, "fname": "C", "lname": "Y" }] ;
    
    var empIdObj={};
    
    empid.forEach(function(element) {
    empIdObj[element]=true;
    });
    
    var filteredArray=[];
    
    records.forEach(function(element) {
    if(empIdObj[element.empid])
        filteredArray.push(element)
    });
    
    0 讨论(0)
  • 2020-11-28 08:39

    This is a fast solution with a temporary object.

    var records = [{ "empid": 1, "fname": "X", "lname": "Y" }, { "empid": 2, "fname": "A", "lname": "Y" }, { "empid": 3, "fname": "B", "lname": "Y" }, { "empid": 4, "fname": "C", "lname": "Y" }, { "empid": 5, "fname": "C", "lname": "Y" }],
        empid = [1, 4, 5],
        object = {},
        result;
    
    records.forEach(function (a) {
        object[a.empid] = a;
    });
    
    result = empid.map(function (a) {
        return object[a];
    });
    document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

    0 讨论(0)
  • 2020-11-28 08:46

    In case you have key value pairs in your input array, I used:

    .filter(
              this.multi_items[0] != null && store.state.isSearchBox === false
                ? item =>
                    _.map(this.multi_items, "value").includes(item["wijknaam"])
                : item => item["wijknaam"].includes("")
            );
    

    where the input array is multi_items as: [{"text": "bla1", "value": "green"}, {"text": etc. etc.}]

    _.map is a lodash function.

    0 讨论(0)
提交回复
热议问题