繁体   English   中英

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

[英]Return matching objects from array of objects

我想根据搜索字符串从给定数组中搜索匹配的值对象。例如,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' }
];

现在我只想搜索那些具有Object作为值的数组。 意味着它应该返回我0,1&3对象

帮助将不胜感激

您可以使用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;
});

您可以遍历数组中的每个对象,获取每个对象的键以防止自己具有该对象的动态属性,然后检查该键的值是否具有Object作为子字符串。 请注意,以下代码中的obj[key].toString() 之所以使用toString()是因为您的id具有整数值,而indexOf()可以处理字符串值。 因此,使用它将防止错误。

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

您可以通过在Arrays中使用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 resultArray = objArray.filter(function (d) { 
    return Object.values(d).indexOf('Object 0') != -1 
});

filter()方法中的d值从对象数组中获取每个对象。 Object.values()返回与对象中的每个keys关联的值的数组。 indexOf()本质上检查要匹配的字符串在数组中是否可用。 如果匹配,则将结果放入resultArray否则将被忽略。 最终的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"}
]

您可以使用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