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

Javascript 从对象数组返回匹配的对象,javascript,arrays,Javascript,Arrays,我想从给定数组中搜索匹配值对象,具体取决于搜索字符串。例如,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

我想从给定数组中搜索匹配值对象,具体取决于搜索字符串。例如,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' }
];
现在我只想搜索那些以
对象
作为值的数组。意味着它应该返回0,1和3对象

非常感谢您的帮助

您可以通过在阵列中使用来完成此操作

这是你要找的东西的样本

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()
返回与对象中每个
键相关联的值数组。
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);

您可以使用
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()
<之所以使用code>toString()
,是因为您的
id
具有整数值,并且
indexOf()
对字符串值起作用。因此,使用它可以防止错误

var objArray=[
{id:0,name:'Object 0',otherProp:'321',secondVal:'stack'},
{id:1,名称:'O1',其他属性:'Object 0',secondVal:'Overflow'},
{id:2,name:'otherobject',otherProp:'850',secondVal:'Context'},
{id:3,name:'差不多了',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);
}
});
});

控制台日志(res)和-换句话说
objArray.filter(item=>Object.values(item).includes('object0'))使用
array.filter的工作示例
@Pradeep Jain,如果我只想搜索“Object”怎么办?它让我感到空虚array@PrasannaSasne可以使用简单循环。检查答案我不想搜索完全匹配的答案。搜索字符串也可以是小写。我试过使用“==”但不起作用。它返回为空数组
=
不忽略大小写。您必须将字符串转换为小写以便比较。只需将if语句替换为:
if(obj[key].toLowerCase()=='object 0')
。对于其他比较,您可能必须使用正则表达式。如果您想使用正则表达式,可以使用javscript函数替换If语句:
If(obj[key].match(/object/i)){…