最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to filter array of objects and then return a specific attribute? - Stack Overflow

matteradmin6PV0评论

consider the data :

let orders = {
        "data": [
            {
                "email": "[email protected]", "orders": [
                    { "orderName": "something", "price": "43$" },
                    { "orderName": "anotherthing", "price": "4$" }
                ]
            },{
                "email": "[email protected]", "orders": [
                    { "orderName": "fish", "price": "43$" },
                    { "orderName": "parrot", "price": "4$" }
                ]
            }
        ]
    };

I'm trying to filter the orders of the object with some email like:

email = '[email protected]'
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.

consider the data :

let orders = {
        "data": [
            {
                "email": "[email protected]", "orders": [
                    { "orderName": "something", "price": "43$" },
                    { "orderName": "anotherthing", "price": "4$" }
                ]
            },{
                "email": "[email protected]", "orders": [
                    { "orderName": "fish", "price": "43$" },
                    { "orderName": "parrot", "price": "4$" }
                ]
            }
        ]
    };

I'm trying to filter the orders of the object with some email like:

email = '[email protected]'
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.

Share Improve this question edited Jun 11, 2019 at 13:50 Harel.Lebel asked Jun 11, 2019 at 13:34 Harel.LebelHarel.Lebel 2896 silver badges17 bronze badges 3
  • 3 why not just map after filter? – Austaras Commented Jun 11, 2019 at 13:36
  • Possible duplicate of From an array of objects, extract value of a property as array – Heretic Monkey Commented Jun 11, 2019 at 13:42
  • filter just checks whether the value returned by the function is truthy or not, it doesn't use it as the result. – Barmar Commented Jun 11, 2019 at 13:51
Add a ment  | 

4 Answers 4

Reset to default 6

You can't do with filter alone, you also need to map:

orders.data.filter(o => o.email === '[email protected]').map(o => o.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.

You can chain Array#filter with Array#map to produce the result:

let orders = { "data": [{ "email": "[email protected]", "orders": [{ "orderName": "something", "price": "43$" }, { "orderName": "anotherthing", "price": "4$" } ] }, { "email": "[email protected]", "orders": [{ "orderName": "fish", "price": "43$" }, { "orderName": "parrot", "price": "4$" } ] }]};

email = '[email protected]';
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:

let orders = { "data": [{ "email": "[email protected]", "orders": [{ "orderName": "something", "price": "43$" }, { "orderName": "anotherthing", "price": "4$" } ] }, { "email": "[email protected]", "orders": [{ "orderName": "fish", "price": "43$" }, { "orderName": "parrot", "price": "4$" } ] }]};

email = '[email protected]';
x = orders.data
  .find(o => o.email === email)
  .orders;
  
console.log(x);

An alternative is to use find and then just reference orders when needed. Adding || {'orders': 'Email not found'}; after will catch if the email isn't found

let orders = {
  "data": [{
    "email": "[email protected]",
    "orders": [{
        "orderName": "something",
        "price": "43$"
      },
      {
        "orderName": "anotherthing",
        "price": "4$"
      }
    ]
  }, {
    "email": "[email protected]",
    "orders": [{
        "orderName": "fish",
        "price": "43$"
      },
      {
        "orderName": "parrot",
        "price": "4$"
      }
    ]
  }]
};

email = '[email protected]'
x = orders.data.find(o => {
  return o.email === email
}) || {
  'orders': 'Email not found'
};
console.log(x.orders);
email = '[email protected]'
x = orders.data.find(o => {
  return o.email === email
}) || {
  'orders': 'Email not found'
};
console.log(x.orders);

let orders = {
        "data": [
            {
                "email": "[email protected]", "orders": [
                    { "orderName": "something", "price": "43$" },
                    { "orderName": "anotherthing", "price": "4$" }
                ]
            },{
                "email": "[email protected]", "orders": [
                    { "orderName": "fish", "price": "43$" },
                    { "orderName": "parrot", "price": "4$" }
                ]
            }
        ]
    };
    
const filteredOrders = orders.data.map((o) => o.email === '[email protected]' ? o.orders : null).filter(o => o);

console.log(filteredOrders)

You can map first too and filter after that the valid results.

Post a comment

comment list (0)

  1. No comments so far