简体   繁体   中英

Return matching objects from array of objects

I want to search matching value objects from given array depending of searchString.eg seatch string would be 'Object 0'.

var objArray = [
   { id: 0, name: 'Object 0', otherProp: '321', secondVal:'stack' },
   { id: 1, name: 'O1', otherProp: 'Object 0', secondVal: 'Overflow' },
   { id: 2, name: 'Another Object', otherProp: '850', secondVal: 'Context' },
   { id: 3, name: 'Almost There', otherProp: '046', secondVal: 'Object 1' },
   { id: 4, name: 'Last Obj', otherProp: '78', secondVal: 'test' }
];

Now i want to search only those array which are having Object as value. Means it should return me 0,1 & 3 object

Help will be appreciated

You could do that with the array.filter method and a loop over all properties within this filter method.

var objArray = [
   { id: 0, name: 'Object 0', otherProp: '321', secondVal:'stack' },
   { id: 1, name: 'O1', otherProp: 'Object 0', secondVal: 'Overflow' },
   { id: 2, name: 'Another Object', otherProp: '850', secondVal: 'Context' },
   { id: 3, name: 'Almost There', otherProp: '046', secondVal: 'Object 0' },
   { id: 4, name: 'Last Obj', otherProp: '78', secondVal: 'test' }
];

var filteredArray = objArray.filter((obj) => {
  for (var key in obj) {
    if (obj[key] === 'Object 0') {
      return true;
    }
  }
  return false;
});

You can loop through each object in the array, get the keys for each object to prevent yourself from dynamic properties in the object, and then check if the value for that key has Object as a substring or not. Note that obj[key].toString() in the below code. toString() is used because your id has integer value and indexOf() works on string value. Thus, using that will prevent from error.

 var objArray = [ { id: 0, name: 'Object 0', otherProp: '321', secondVal:'stack' }, { id: 1, name: 'O1', otherProp: 'Object 0', secondVal: 'Overflow' }, { id: 2, name: 'Another Object', otherProp: '850', secondVal: 'Context' }, { id: 3, name: 'Almost There', otherProp: '046', secondVal: 'Object 1' }, { id: 4, name: 'Last Obj', otherProp: '78', secondVal: 'test' } ]; var res = []; objArray.filter((obj) => { Object.keys(obj).forEach((key)=>{ if(obj[key].toString().indexOf('Object') !== -1){ res.push(obj); } }); }); console.log(res); 

You can do this by using filter() in Arrays.

Here's a sample of what you're looking for

var objArray = [
   { id: 0, name: 'Object 0', otherProp: '321', secondVal:'stack' },
   { id: 1, name: 'O1', otherProp: 'Object 0', secondVal: 'Overflow' },
   { id: 2, name: 'Another Object', otherProp: '850', secondVal: 'Context' },
   { id: 3, name: 'Almost There', otherProp: '046', secondVal: 'Object 0' },
   { id: 4, name: 'Last Obj', otherProp: '78', secondVal: 'test' }
];

var resultArray = objArray.filter(function (d) { 
    return Object.values(d).indexOf('Object 0') != -1 
});

The value of d in the filter() method takes each object from your array of object. Object.values() returns an array of the values associated with each of the keys in the object. The indexOf() essentially checks if the string you want to match is available in the array. In case it matches, the result is put in resultArray else it's ignored. The final resultArray looks as follows:

[
    {id: 0, name: "Object 0", otherProp: "321", secondVal: "stack"},
    {id: 1, name: "O1", otherProp: "Object 0", secondVal: "Overflow"},
    {id: 3, name: "Almost There", otherProp: "046", secondVal: "Object 0"}
]

You can use array.filter() and array.includes() , Object.values() to do this.

var objArray = [
      {id: 0, name: 'Object 0', otherProp: '321', secondVal: 'stack'},
      {id: 1, name: 'O1', otherProp: 'Object 0', secondVal: 'Overflow'},
      {id: 2, name: 'Another Object', otherProp: '850', secondVal: 'Context'},
      {id: 3, name: 'Almost There', otherProp: '046', secondVal: 'Object 0'},
      {id: 4, name: 'Last Obj', otherProp: '78', secondVal: 'test'},
    ];


const result = objArray.filter((object) => {
  const values = Object.values(object);
  return values.includes('Object 0');
});

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