简体   繁体   English

使用属性值从对象数组中查找并返回 object

[英]Find and Return an object from an array of array of objects using a property value

I have an array of array of objects.我有一组对象数组。 Is there an ES6 way to find and return an object based on a property value?是否有 ES6 方法可以根据属性值查找并返回 object?

const tempArray = [];
tempArray.push([{ name: 'apple', y: 1}, { name: 'orange', y: 2}]);
tempArray.push([{ name: 'pear', y: 3}]);

Given the value property name of 'apple', I want the object { name: 'apple', y: 1} returned.给定“apple”的值属性名称,我希望返回 object { name: 'apple', y: 1}

I've tried tempArray.filter(k => k.some(e => e.name === 'apple')) but it returns an array of array of objects which I don't want.我试过tempArray.filter(k => k.some(e => e.name === 'apple'))但它返回一个我不想要的对象数组。

Thanks for your help in advance,提前感谢您的帮助,

Flatten the array, then use .find to find the matching object:将数组展平,然后使用.find找到匹配的 object:

 const tempArray = []; tempArray.push([{ name: 'apple', y: 1}, { name: 'orange', y: 2}]); tempArray.push([{ name: 'pear', y: 3}]); const obj = tempArray.flat().find(({ name }) => name === 'apple'); console.log(obj);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在对象数组中的属性中查找具有最大值的所有对象,并从同一对象返回其他属性的值 - Finding all objects with Max value in a property within an Array of Objects and return values of other property from the same object 将对象的属性值添加到对象数组 - Add property value from object to array of objects 从对象数组获取对象属性值 - Get object property value from array of objects LoDash从子数组中找到其属性与对象数组中的值匹配的对象 - LoDash find the object whose property matches the value from array of objects with children array Javascript从属性值匹配的对象数组返回数组 - Javascript return array from array of objects where property value matches 使用lodash从数组返回对象属性 - return object property using lodash from array 如何通过嵌套在对象数组的属性中的对象属性的值在对象数组中找到多个索引? - How to find multiple indexes in array of objects by the value of an object property that nests in a property of array of objects? 在对象数组中查找具有 id 属性最大值的对象 - Find object having maximum value for the `id` property in an array of objects 使用具有ng-repeat的对象数组中的另一个对象的id属性,通过id查找对象 - Find object by id using another object's id property from array of objects with ng-repeat Javascript 过滤数组中的对象并返回数组中 Object 中 Object 的属性 - Javascript Filter Objects in Array and return Property of Object in Array of Object in Array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM