简体   繁体   English

如何过滤对象数组,然后返回特定属性?

[英]How to filter array of objects and then return a specific attribute?

consider the data : 考虑数据:

let orders = {
        "data": [
            {
                "email": "a@b.com", "orders": [
                    { "orderName": "something", "price": "43$" },
                    { "orderName": "anotherthing", "price": "4$" }
                ]
            },{
                "email": "c@w.com", "orders": [
                    { "orderName": "fish", "price": "43$" },
                    { "orderName": "parrot", "price": "4$" }
                ]
            }
        ]
    };

I'm trying to filter the orders of the object with some email like: 我正在尝试通过一些电子邮件来过滤对象的顺序,例如:

email = 'a@b.com'
x=orders.data.filter(o =>{if (o.email === email) return o.orders});

but the whole return value is the whole matching object, with email and orders, and I don't want the whole object , I want only the orders. 但是整个返回值是整个匹配的对象,带有电子邮件和订单,并且我不需要整个对象,我只需要订单。

您不能单独使用filter ,还需要map

orders.data.filter(o => o.email === 'a@b.com').map(o => o.orders)

An alternative is to use find and then just reference orders when needed. 一种替代方法是使用find ,然后在需要时仅参考orders Adding || {'orders': 'Email not found'}; 添加|| {'orders': 'Email not found'}; || {'orders': 'Email not found'}; after will catch if the email isn't found 如果找不到电子邮件,将在之后捕获

 let orders = { "data": [{ "email": "a@b.com", "orders": [{ "orderName": "something", "price": "43$" }, { "orderName": "anotherthing", "price": "4$" } ] }, { "email": "c@w.com", "orders": [{ "orderName": "fish", "price": "43$" }, { "orderName": "parrot", "price": "4$" } ] }] }; email = 'a@b.com' x = orders.data.find(o => { return o.email === email }) || { 'orders': 'Email not found' }; console.log(x.orders); email = 'x@y.com' x = orders.data.find(o => { return o.email === email }) || { 'orders': 'Email not found' }; console.log(x.orders); 

You cannot do this with .filter as the method will only return a subsection if the array, while you want to also transform it. 您无法使用.filter做到这一点,因为该方法只会在数组也要返回子节的情况下,同时还希望对其进行转换。

You can chain Array#filter with Array#map to produce the result: 您可以将Array#filterArray#map以产生结果:

 let orders = { "data": [{ "email": "a@b.com", "orders": [{ "orderName": "something", "price": "43$" }, { "orderName": "anotherthing", "price": "4$" } ] }, { "email": "c@w.com", "orders": [{ "orderName": "fish", "price": "43$" }, { "orderName": "parrot", "price": "4$" } ] }]}; email = 'a@b.com'; x = orders.data .filter(o => o.email === email) .map(o => o.orders); console.log(x); 

If you expect a single element here, you can use Array#find instead: 如果希望在此处使用单个元素,则可以改用Array#find

 let orders = { "data": [{ "email": "a@b.com", "orders": [{ "orderName": "something", "price": "43$" }, { "orderName": "anotherthing", "price": "4$" } ] }, { "email": "c@w.com", "orders": [{ "orderName": "fish", "price": "43$" }, { "orderName": "parrot", "price": "4$" } ] }]}; email = 'a@b.com'; x = orders.data .find(o => o.email === email) .orders; console.log(x); 

 let orders = { "data": [ { "email": "a@b.com", "orders": [ { "orderName": "something", "price": "43$" }, { "orderName": "anotherthing", "price": "4$" } ] },{ "email": "c@w.com", "orders": [ { "orderName": "fish", "price": "43$" }, { "orderName": "parrot", "price": "4$" } ] } ] }; const filteredOrders = orders.data.map((o) => o.email === 'a@b.com' ? o.orders : null).filter(o => o); console.log(filteredOrders) 

You can map first too and filter after that the valid results. 您也可以先映射,然后再过滤有效结果。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM