user9686855
user9686855

Reputation:

How to return not all of the properties of an object from arrays with an array method?

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'}]

Upvotes: 0

Views: 381

Answers (4)

Bálint
Bálint

Reputation: 4039

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.

Upvotes: 1

Faly
Faly

Reputation: 13346

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);

Upvotes: 0

joseph oun
joseph oun

Reputation: 135

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}
} );

Upvotes: -4

T.J. Crowder
T.J. Crowder

Reputation: 1074238

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.

Upvotes: 3

Related Questions