0

I want to use an array method with arrow function. For an example:

const inventory = [
    {name: 'apples', quantity: 2, type: 'a'},
    {name: 'bananas', quantity: 0, type: 'a'},
    {name: 'cherries', quantity: 5, type: 'a'}
    {name: 'mangos', quantity: 5, type: 'a'}
];

const result = inventory.filter( fruit => fruit.quantity === 5 );

What if i want to return just the object with the name and type properties? Like this: console.log(result) //[{name: 'mangos', type: 'a'}, {name: 'cherries', type: 'a'}]

  • 3
    If you want a new object with fewer properties, then return a new object. Seems like you first want to `.filter()` with the given predicate before mapping the new objects though. –  May 14 '18 at 11:55
  • 2
    FYI, your line `inventory.map( fruit => fruit.quantity === 5 );` will return an array of `true`/`false` values. Not sure if that's what you're after there. – Máté Safranka May 14 '18 at 11:56
  • 3
    `map(({ quantity, ...rest }) => rest)` – Andrew Li May 14 '18 at 11:57
  • 1
    @CrazyTrain `rest` is already an object, no need to create an object from an object. – Andrew Li May 14 '18 at 11:58
  • @Li357: Ah yeah, you're right. For some reason I thought it returned a temporary collection like `.entries()` returns. –  May 14 '18 at 12:00
  • Possible duplicate of [How to remove properties from an object array?](https://stackoverflow.com/questions/37222885/how-to-remove-properties-from-an-object-array) – Sebastian Simon May 14 '18 at 12:01

4 Answers4

3

You'd create a new object. It looks like you want to do two things, though: Filter to only items with quantity of 5, and return objects without the quantity field. Unelss you have hundreds of thousands of these¹, you'd do that by using filter then map. Here's an example with destructuring:

const inventory = [
    {name: 'apples', quantity: 2, type: 'a'},
    {name: 'bananas', quantity: 0, type: 'a'},
    {name: 'cherries', quantity: 5, type: 'a'},
    {name: 'mangos', quantity: 5, type: 'a'}
];

const result = inventory
  .filter(fruit => fruit.quantity === 5)
  .map(({name, type}) => ({name, type}));

console.log(result);

¹ If you do have hundreds of thousands of these or more, you might consider just making one pass with forEach.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1
inventory.filter(fruit => fruit.quantity === 5).map(fruit => ({ name: fruit.name, type: fruit.type }));

map creates a new array using the values you give it, but doesn't change the original, filter creates an array using only the values the function returned a truthy value for.

Bálint
  • 4,009
  • 2
  • 16
  • 27
0

You can remove the type property by destructuring:

const inventory = [
    {name: 'apples', quantity: 2, type: 'a'},
    {name: 'bananas', quantity: 0, type: 'a'},
    {name: 'cherries', quantity: 5, type: 'a'},
    {name: 'mangos', quantity: 5, type: 'a'}
];

const res = inventory.map(({type: x, ...rest}) => rest);

console.log(res);

Or you can just make your array.map callback returning object having only name and quantity field:

const inventory = [
    {name: 'apples', quantity: 2, type: 'a'},
    {name: 'bananas', quantity: 0, type: 'a'},
    {name: 'cherries', quantity: 5, type: 'a'},
    {name: 'mangos', quantity: 5, type: 'a'}
];

const res = inventory.map(({name, quantity}) => ({name, quantity}));

console.log(res);
Faly
  • 13,291
  • 2
  • 19
  • 37
-4

Try using lodash

const inventory = [
    {name: 'apples', quantity: 2, type: 'a'},
    {name: 'bananas', quantity: 0, type: 'a'},
    {name: 'cherries', quantity: 5, type: 'a'}
    {name: 'mangos', quantity: 5, type: 'a'}
];

const result = ._map( inventory, (item)=> { 
    return {
             name: item.name, 
             quantity: item.quantity}
} );
joseph oun
  • 135
  • 1
  • 7