我希望您能从给定的数组中搜索与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号元素。感谢您的帮助。
objArray.filter(item => Object.values(item).includes('Object 0'));
- Jaromanda Xarray.filter
的工作示例:https://stackblitz.com/edit/angular-csaana?file=src%2Fapp%2Fapp.component.ts - Pardeep Jain