Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate Nested Array after Mapping

I have an array with several category objects, each of which has an items property containing an array of item objects. I want to map each item in each category to an object[] with objects that have the properties value and label. For some reason, I can't perform the concatenation.

var categories = [{
                name: "category1",
                items: [{
                    itemId: 1,
                    name: "Item1"
                }, {
                    itemId: 2,
                    name: "Item2"
                }]
            }, {
                name: "category2",
                items: [{
                    itemId: 3,
                    name: "Item3"
                }, {
                    itemId: 4,
                    name: "Item4"
                }]
            }];

var items = [];
for(var i = 0; i < categories.length; i++){
    items.concat($.map(categories[i].items,function(elem){
        return {value:elem.itemId, label:elem.name};
    }));
} 
console.log(items); //prints []

Expected Result

[{
   label: "Item1",
   value: "1"
},
{
   label: "Item2",
   value: "2"
},{
   label: "Item3",
   value: "3"
},{
   label: "Item4",
   value: "4"
}

I feel as if I am missing something very basic. I logged the result of the $.map function and it appears to be returning an []. Can anyone figure out the issue?

JSFiddle: http://jsfiddle.net/vymJv/

like image 384
Kevin Bowersox Avatar asked Jun 12 '13 15:06

Kevin Bowersox


People also ask

How do you flatten a nested array of objects?

The Array. flat() method is primarily used to flatten a Nested JavaScript array. It accepts the depth level as an argument and returns a new array in which all of the elements of sub-arrays are concatenated according to the specified depth.

Can you concatenate an array?

The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

How do you concatenate two values in an array?

In order to combine (concatenate) two arrays, we find its length stored in aLen and bLen respectively. Then, we create a new integer array result with length aLen + bLen . Now, in order to combine both, we copy each element in both arrays to result by using arraycopy() function.

How do you concatenate arrays into arrays?

The concat() method concatenates (joins) two or more arrays. The concat() method returns a new array, containing the joined arrays. The concat() method does not change the existing arrays.


2 Answers

updated with flatMap(not compatible with IE)

categories.flatMap((categories) => categories.items)

flatMap() method returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level.

like image 183
ncesar Avatar answered Sep 28 '22 21:09

ncesar


  const items = categories
          .map(category => category.items)
          .reduce((prev, current) => [...prev, ...current])
          .map((e, i) => ({ label: e.name, value: e.itemId }));
like image 32
Dailenis González Frómeta Avatar answered Sep 28 '22 20:09

Dailenis González Frómeta