Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to exclude some elements from javascript Array.map() callback

Essentially, I want to implement the following:

var categories = [];
var products = // some array of product objects
products.map(function(value) {
   if(categories.indexOf(value.Category === -1)) categories.push(value.Category);
});

As result, categories array contains unique list of product categories.

I feel that there should be a better way to do it, but nothing comes to mind.

If there isn't then probably there is no point to use map() in the first place. I could do as simple as

var categories = [];
var products = // some array of product objects
for (var i = 0; i < products.length; i++) {
   if(categories.indexOf(products[i].Category === -1)) categories.push(products[i].Category);
}

UPDATE for those who insist it's a duplicate of "how to make an array unique" question. I saw that post, and for my situation I don't think it applies. I don't have an array of values that I need to make unique. I have an array of objects and I need to build an array of unique values. The difference might be subtle - but to get to the use case of that topic I would build a non-unique array and then make it unique. Seems even worse than my original solution

like image 467
Felix Avatar asked Dec 25 '15 19:12

Felix


People also ask

How do you skip elements on a map?

There are various ways to skip over an element in the map: Using if loop inside the function to be executed to add the constraints to skip over that element. Using the filter method. Using the arrow function.

How do you exclude an item from an array?

If you want to remove an item from an array, you can use the pop() method to remove the last element or the shift() method to remove the first element.

How do you remove an element from an array in JavaScript?

Array elements can be deleted using the JavaScript operator delete . Using delete leaves undefined holes in the array. Use pop() or shift() instead.

How do you remove an element from an array filter?

You can remove elements from the end of an array using pop, from the beginning using shift, or from the middle using splice. The advanced way to remove unwanted elements is to use JavaScript Array filter method to create a new array with desired items.


2 Answers

you can use reduce instead of map

var products = [{Category:'vegetable', price: 1}, {Category:'fruits', price: 2}];
var categories = products.reduce(function(sum, product) {
 if(sum.indexOf(product.Category) === -1){
  sum.push(product.Category);
 }
 return sum;
}, []);
like image 87
Bek Avatar answered Oct 02 '22 17:10

Bek


map all the values of the object categories out first, then use filter to dispose of the duplicates.

var products = [
  { category: 'A' },
  { category: 'B' },
  { category: 'A' },
  { category: 'D' }
];

var categories = products.map(function (e) {
  return e.category;
}).filter(function (e, i, a) {
  return a.indexOf(e) === i;
}); // [ "A", "B", "D" ]

DEMO

like image 44
Andy Avatar answered Oct 02 '22 15:10

Andy