How to do equivalent of LINQ SelectMany() just in javascript

JavascriptC#

Javascript Problem Overview


Unfortunately, I don't have JQuery or Underscore, just pure javascript (IE9 compatible).

I'm wanting the equivalent of SelectMany() from LINQ functionality.

// SelectMany flattens it to just a list of phone numbers.
IEnumerable<PhoneNumber> phoneNumbers = people.SelectMany(p => p.PhoneNumbers);

Can I do it?

EDIT:

Thanks to answers, I got this working:

var petOwners = 
[
	{
        Name: "Higa, Sidney", Pets: ["Scruffy", "Sam"]
    },
	{
        Name: "Ashkenazi, Ronen", Pets: ["Walker", "Sugar"]
    },
	{
        Name: "Price, Vernette", Pets: ["Scratches", "Diesel"]
    },
];

function property(key){return function(x){return x[key];}}
function flatten(a,b){return a.concat(b);}

var allPets = petOwners.map(property("Pets")).reduce(flatten,[]);

console.log(petOwners[0].Pets[0]);
console.log(allPets.length); // 6

var allPets2 = petOwners.map(function(p){ return p.Pets; }).reduce(function(a, b){ return a.concat(b); },[]); // all in one line

console.log(allPets2.length); // 6

Javascript Solutions


Solution 1 - Javascript

for a simple select you can use the reduce function of Array.
Lets say you have an array of arrays of numbers:

var arr = [[1,2],[3, 4]];
arr.reduce(function(a, b){ return a.concat(b); }, []);
=>  [1,2,3,4]

var arr = [{ name: "name1", phoneNumbers : [5551111, 5552222]},{ name: "name2",phoneNumbers : [5553333] }];
arr.map(function(p){ return p.phoneNumbers; })
   .reduce(function(a, b){ return a.concat(b); }, [])
=>  [5551111, 5552222, 5553333]

Edit:
since es6 flatMap has been added to the Array prototype. SelectMany is synonym to flatMap.
The method first maps each element using a mapping function, then flattens the result into a new array. Its simplified signature in TypeScript is:

function flatMap<A, B>(f: (value: A) => B[]): B[]

In order to achieve the task we just need to flatMap each element to phoneNumbers

arr.flatMap(a => a.phoneNumbers);

Solution 2 - Javascript

As a simpler option Array.prototype.flatMap() or Array.prototype.flat()

const data = [
{id: 1, name: 'Dummy Data1', details: [{id: 1, name: 'Dummy Data1 Details'}, {id: 1, name: 'Dummy Data1 Details2'}]},
{id: 1, name: 'Dummy Data2', details: [{id: 2, name: 'Dummy Data2 Details'}, {id: 1, name: 'Dummy Data2 Details2'}]},
{id: 1, name: 'Dummy Data3', details: [{id: 3, name: 'Dummy Data3 Details'}, {id: 1, name: 'Dummy Data3 Details2'}]},
]

const result = data.flatMap(a => a.details); // or data.map(a => a.details).flat(1);
console.log(result)

Solution 3 - Javascript

For those a while later, understanding javascript but still want a simple Typed SelectMany method in Typescript:

function selectMany<TIn, TOut>(input: TIn[], selectListFn: (t: TIn) => TOut[]): TOut[] {
  return input.reduce((out, inx) => {
    out.push(...selectListFn(inx));
    return out;
  }, new Array<TOut>());
}

Solution 4 - Javascript

Sagi is correct in using the concat method to flatten an array. But to get something similar to this example, you would also need a map for the select part https://msdn.microsoft.com/library/bb534336(v=vs.100).aspx

/* arr is something like this from the example PetOwner[] petOwners = 
                    { new PetOwner { Name="Higa, Sidney", 
                          Pets = new List<string>{ "Scruffy", "Sam" } },
                      new PetOwner { Name="Ashkenazi, Ronen", 
                          Pets = new List<string>{ "Walker", "Sugar" } },
                      new PetOwner { Name="Price, Vernette", 
                          Pets = new List<string>{ "Scratches", "Diesel" } } }; */

function property(key){return function(x){return x[key];}}
function flatten(a,b){return a.concat(b);}

arr.map(property("pets")).reduce(flatten,[])

Solution 5 - Javascript

// you can save this function in a common js file of your project
function selectMany(f){ 
    return function (acc,b) {
        return acc.concat(f(b))
    }
}

var ex1 = [{items:[1,2]},{items:[4,"asda"]}];
var ex2 = [[1,2,3],[4,5]]
var ex3 = []
var ex4 = [{nodes:["1","v"]}]

Let's start

ex1.reduce(selectMany(x=>x.items),[])

=> [1, 2, 4, "asda"]

ex2.reduce(selectMany(x=>x),[])

=> [1, 2, 3, 4, 5]

ex3.reduce(selectMany(x=> "this will not be called" ),[])

=> []

ex4.reduce(selectMany(x=> x.nodes ),[])

=> ["1", "v"]

NOTE: use valid array (non null) as intitial value in the reduce function

Solution 6 - Javascript

try this (with es6):

 Array.prototype.SelectMany = function (keyGetter) {
 return this.map(x=>keyGetter(x)).reduce((a, b) => a.concat(b)); 
 }

example array :

 var juices=[ {key:"apple",data:[1,2,3]},
 {key:"banana",data:[4,5,6]},
 {key:"orange",data:[7,8,9]}
 ]

using :

juices.SelectMany(x=>x.data)

Solution 7 - Javascript

I would do this (avoiding .concat()):

function SelectMany(array) {
    var flatten = function(arr, e) {
        if (e && e.length)
            return e.reduce(flatten, arr);
        else 
            arr.push(e);
        return arr;
    };

    return array.reduce(flatten, []);
}

var nestedArray = [1,2,[3,4,[5,6,7],8],9,10];
console.log(SelectMany(nestedArray)) //[1,2,3,4,5,6,7,8,9,10]

If you don't want to use .reduce():

function SelectMany(array, arr = []) {
    for (let item of array) {
        if (item && item.length)
            arr = SelectMany(item, arr);
        else
            arr.push(item);
    }
    return arr;
}

If you want to use .forEach():

function SelectMany(array, arr = []) {
    array.forEach(e => {
        if (e && e.length)
            arr = SelectMany(e, arr);
        else
            arr.push(e);
    });

    return arr;
}

Solution 8 - Javascript

Here you go, a rewritten version of joel-harkes' answer in TypeScript as an extension, usable on any array. So you can literally use it like somearray.selectMany(c=>c.someprop). Trans-piled, this is javascript.

declare global {
    interface Array<T> {
        selectMany<TIn, TOut>(selectListFn: (t: TIn) => TOut[]): TOut[];
    }
}

Array.prototype.selectMany = function <TIn, TOut>( selectListFn: (t: TIn) => TOut[]): TOut[] {
    return this.reduce((out, inx) => {
        out.push(...selectListFn(inx));
        return out;
    }, new Array<TOut>());
}


export { };

Solution 9 - Javascript

You can try the manipula package that implements all C# LINQ methods and preserves its syntax:

Manipula.from(petOwners).selectMany(x=>x.Pets).toArray()

https://github.com/litichevskiydv/manipula

https://www.npmjs.com/package/manipula

Solution 10 - Javascript

For later versions of JavaScript you can do this:

  var petOwners = [    {      Name: 'Higa, Sidney',      Pets: ['Scruffy', 'Sam']
    },
    {
      Name: 'Ashkenazi, Ronen',
      Pets: ['Walker', 'Sugar']
    },
    {
      Name: 'Price, Vernette',
      Pets: ['Scratches', 'Diesel']
    }
  ];

  var arrayOfArrays = petOwners.map(po => po.Pets);
  var allPets = [].concat(...arrayOfArrays);

  console.log(allPets); // ["Scruffy","Sam","Walker","Sugar","Scratches","Diesel"]

See example StackBlitz.

Solution 11 - Javascript

Exception to reduce and concat methods, you can use the native flatMap api.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontoddmoView Question on Stackoverflow
Solution 1 - JavascriptSagiView Answer on Stackoverflow
Solution 2 - JavascriptNecip SunmazView Answer on Stackoverflow
Solution 3 - JavascriptJoel HarkesView Answer on Stackoverflow
Solution 4 - JavascriptFabio BeltraminiView Answer on Stackoverflow
Solution 5 - JavascriptBogdan ManoleView Answer on Stackoverflow
Solution 6 - JavascriptCem TuğutView Answer on Stackoverflow
Solution 7 - JavascriptWesleyACView Answer on Stackoverflow
Solution 8 - JavascriptWorthy7View Answer on Stackoverflow
Solution 9 - JavascriptrazonView Answer on Stackoverflow
Solution 10 - JavascriptGary McGillView Answer on Stackoverflow
Solution 11 - JavascriptKamil OZTURKView Answer on Stackoverflow