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

时间:2019-06-11 13:34:28

标签: javascript

考虑数据:

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 =>{if (o.email === email) return o.orders});

但是整个返回值是整个匹配的对象,包含电子邮件和订单,我不希望整个对象,我只想要订单。

4 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

一种替代方法是使用find,然后在需要时仅引用orders。如果找不到电子邮件,则在之后添加|| {'orders': 'Email not 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);

答案 2 :(得分:0)

您无法使用.filter来执行此操作,因为该方法只会在数组同时返回子节,同时您还希望对其进行转换。

您可以将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);

如果希望在此处使用单个元素,则可以改用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);

答案 3 :(得分:0)

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)

您也可以先映射,然后再过滤有效结果。