Skip to content Skip to sidebar Skip to footer

Filter Array Of Objects That Contains String Using Lodash

I have a value and i need to return the objects that contains this value in propertie. var search='CPP@'; var results=_.filter(collection,{VAL:search}); I need to grab all objects

Solution 1:

var collection=[
  {
    "DSP_MAQ": "Máquina 4",
    "VAL": "CPP@4@1900-01-01"
  },
  {
    "DSP_MAQ": "Máquina 5",
    "VAL": "CMIP@5@1900-01-01"
  },
  {
    "DSP_MAQ": "Máquina 6",
    "VAL": "CMIP@6@1900-01-01"
  },
  {
    "DSP_MAQ": "Máquina 7",
    "VAL": "CMIP@7@1900-01-01"
  },
  {
    "DSP_MAQ": "Máquina 8",
    "VAL": "CPP@8@1900-01-01"
  },
  {
    "DSP_MAQ": "Máquina 9",
    "VAL": "CMIP@9@1900-01-01"
  },
  {
    "DSP_MAQ": "Máquina 10",
    "VAL": "CMIP@10@1900-01-01"
  }
 
];
 var search='CPP@';
    var results=_.filter(collection,function(item){
    return item.VAL.indexOf(search)>-1;
    });
    console.log(results);
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js"></script>
var search='CPP@';
    var results=_.filter(collection,function(item){
    return item.VAL.indexOf(search)>-1;
    });
    console.log(results);

Solution 2:

var results = _.filter(collection,function(obj) {
    return obj.VAL.indexOf(search) !== -1;
});

Solution 3:

var collection=[
  {
    "DSP_MAQ": "Máquina 4",
    "VAL": "CPP@4@1900-01-01"
  },
  {
    "DSP_MAQ": "Máquina 5",
    "VAL": "CMIP@5@1900-01-01"
  },
  {
    "DSP_MAQ": "Máquina 6",
    "VAL": "CMIP@6@1900-01-01"
  },
  {
    "DSP_MAQ": "Máquina 7",
    "VAL": "CMIP@7@1900-01-01"
  },
  {
    "DSP_MAQ": "Máquina 8",
    "VAL": "CPP@8@1900-01-01"
  },
  {
    "DSP_MAQ": "Máquina 9",
    "VAL": "CMIP@9@1900-01-01"
  },
  {
    "DSP_MAQ": "Máquina 10",
    "VAL": "CMIP@10@1900-01-01"
  }
 
];
 var search='CMIP@9@1900-01-01';
var filterList = collection.filter(function(item) {
 return item.VAL === search;
})
console.log(filterList)
use javascirpt filter.

Post a Comment for "Filter Array Of Objects That Contains String Using Lodash"