Prasanna
Prasanna

Reputation: 1751

Return matching objects from array of objects

I want to search matching value objects from a given array depending on searchString. e.g., the search 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 in the array with Object as a value. Means it should return me 0, 1 & 3 object

Help will be appreciated

Upvotes: 3

Views: 4132

Answers (4)

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

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);

Upvotes: 1

Benjamin Lüscher
Benjamin Lüscher

Reputation: 412

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;
});

Upvotes: 1

Anuradha Gunasekara
Anuradha Gunasekara

Reputation: 6983

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);

Upvotes: 0

Sudheesh Singanamalla
Sudheesh Singanamalla

Reputation: 2297

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"}
]

Upvotes: 0

Related Questions