简体   繁体   English

从对象数组返回匹配的对象

[英]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'. 我想根据搜索字符串从给定数组中搜索匹配的值对象。例如,seatch字符串将为“对象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. 现在我只想搜索那些具有Object作为值的数组。 Means it should return me 0,1 & 3 object 意味着它应该返回我0,1&3对象

Help will be appreciated 帮助将不胜感激

You could do that with the array.filter method and a loop over all properties within this filter method. 您可以使用array.filter方法并在此filter方法内的所有属性上循环来完成此操作。

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. 您可以遍历数组中的每个对象,获取每个对象的键以防止自己具有该对象的动态属性,然后检查该键的值是否具有Object作为子字符串。 Note that obj[key].toString() in the below code. 请注意,以下代码中的obj[key].toString() toString() is used because your id has integer value and indexOf() works on string value. 之所以使用toString()是因为您的id具有整数值,而indexOf()可以处理字符串值。 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. 您可以通过在Arrays中使用filter()来实现。

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. filter()方法中的d值从对象数组中获取每个对象。 Object.values() returns an array of the values associated with each of the keys in the object. Object.values()返回与对象中的每个keys关联的值的数组。 The indexOf() essentially checks if the string you want to match is available in the array. indexOf()本质上检查要匹配的字符串在数组中是否可用。 In case it matches, the result is put in resultArray else it's ignored. 如果匹配,则将结果放入resultArray否则将被忽略。 The final resultArray looks as follows: 最终的resultArray如下所示:

[
    {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. 您可以使用array.filter()array.includes()Object.values()来做到这一点。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM