【发布时间】:2018-10-17 23:59:52
【问题描述】:
我想根据 searchString 从给定数组中搜索匹配的值对象。例如,座位字符串将是“对象 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 和 Object.values - 换句话说,
objArray.filter(item => Object.values(item).includes('Object 0')); -
工作示例stackblitz.com/edit/… 使用
array.filter -
@Pradeep Jain,如果我只想搜索“对象”怎么办?它返回给我空数组
-
@PrasannaSasne 你可以使用简单的循环。检查答案
标签: javascript arrays