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

3

我希望您能从给定的数组中搜索与searchString匹配的值对象。例如,搜索字符串可能是“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' }
];

现在我想在数组中搜索值为Object的元素,也就是说,它应该返回0、1和3号元素。
感谢您的帮助。

Array#filterObject.values - 换句话说,objArray.filter(item => Object.values(item).includes('Object 0')); - Jaromanda X
使用 array.filter 的工作示例:https://stackblitz.com/edit/angular-csaana?file=src%2Fapp%2Fapp.component.ts - Pardeep Jain
@Pradeep Jain,如果我只想搜索“Object”,它会返回一个空数组。 - Prasanna
@PrasannaSasne,您可以使用简单的循环。检查答案。 - Ankit Agarwal
4个回答

1
你可以使用array.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;
});

我不想搜索完全匹配。搜索字符串也可能是小写的。我尝试使用“==”,但它不起作用。它返回为空数组。 - Prasanna
== 不会忽略大小写。您必须将字符串转换为小写进行比较。只需用以下内容替换 if 语句:if (obj[key].toLowerCase() === 'object 0')。对于其他比较,您可能需要使用正则表达式。 - Benjamin Lüscher
如果您想使用正则表达式,可以通过Javascript match函数替换if语句:if (obj[key].match(/object/i)) { ... - Benjamin Lüscher

1
你可以循环遍历数组中的每个对象,获取每个对象的键以防止对象中的动态属性,然后检查该键的值是否包含“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);


0

你可以使用数组中的 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() 返回与对象中每个 key 关联的值的数组。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"}
]

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接