1

I have an array like the following which is computed from another function I don't want to modify.

var d = [[1, 2, 3], [2, 3], [1, 3]];

I want to use _.intersection with all the arrays within the above array like this:

var c = _.intersection(d);
// should return [3]

but _.intersection takes multiple array parameters as input, not an array of arrays.

I have tried to flatten an array using something similar to Merge/flatten an array of arrays in JavaScript? but it convert the whole array into a single array.

var merged = [].concat.apply([], d);    
[1, 2, 3, 2, 3, 1, 3]; // I don't want this 

Let me explain this in detail. I have a nested array d (which contains arrays and length is dynamic). Now I want to find the intersection of all arrays that are within array d.

How can I call _.intersection([1, 2, 3], [2, 3], [1, 3], ...) but dynamically with d?

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Rakesh Soni
  • 1,293
  • 2
  • 11
  • 27
  • 4
    Your desired result isn't a data structure. It's 3 separate values. If you want to extract each value individually, there's nothing stopping you from doing that. How do you intend to interact with that data after you've "flattened" it? – Mike Cluck Jun 06 '17 at 16:46
  • you cant set `d = [1,2,3], [2,3], [1,3]` . Please update your question to clearify what it is you're trying to do. – Rooster Jun 06 '17 at 16:47
  • @Rooster updated – Rakesh Soni Jun 06 '17 at 16:49
  • 2
    @RakeshSoni In response to your edit: so you want a function to return 3 values? It can't; it can only return one value. Again, how would you even interact with those 3 values? Pretend you've got the data in the format you want. Show us how you would use it. – Mike Cluck Jun 06 '17 at 16:49
  • @RakeshSoni ^ The answers posted might be what you want, but its still unclear what it is youre trying to do. – Rooster Jun 06 '17 at 16:50
  • Updated the question again – Rakesh Soni Jun 06 '17 at 16:55
  • 2
    @RakeshSoni You still haven't answered my question. Give an example of you using the data. If you want an ordered list of some kind of data, you need an array. If you want to access individual items, you just use normal array indexing. What you appear to be describing does not and cannot exist since you wouldn't have a way of referencing the data. – Mike Cluck Jun 06 '17 at 17:01
  • 2
    Can you include the expected result (valid JavaScript please)? Also, what does an intersection have to do with this? – le_m Jun 06 '17 at 17:01
  • 1
    Updated in detail, hope you all will understand – Rakesh Soni Jun 06 '17 at 17:19

3 Answers3

3

In ES5 you can use Function#apply to spread the array into parameters:

var d = [[1, 2, 3], [2, 3], [1, 3]];

var result = _.intersection.apply(_, d);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

You can use lodash's _.spread that will convert a function to consume an array of parameters:

var d = [[1, 2, 3], [2, 3], [1, 3]];

var result = _.spread(_.intersection)(d);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

In ES6 you can use the spread syntax to do the same thing:

const d = [[1, 2, 3], [2, 3], [1, 3]];

const result = _.intersection(...d);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
2

Your desired result is not a data structure js would support. However, you can destructure the arrays into variables:

let [a,b,c] = [[1, 2, 3], [2, 3], [1, 3]];
console.log(a,b,c);

The only valid representation of what you want to get is a string. You can get the exact result you've asked for like that :-) :

let d = [
  [1, 2, 3],
  [2, 3],
  [1, 3]
];

function createString(arr) {
  let s = arr.map(el => `[${el}]`).join(", ");
  return `result ${s};`;
}

console.log(createString(d));
baao
  • 71,625
  • 17
  • 143
  • 203
0

The only way to do that is to put each of those internal arrays into a separate variable. With ES6, it's as easy as:

let d = [[1, 2, 3], [2, 3], [1, 3]];
let [arr1, arr2, arr3] = d;
// -> arr1: [1, 2, 3], arr2: [2, 3], arr3: [1, 3]

Otherwise, you can do it using the standard ES5 syntax of course.

The only way you'll be able to work with all of those internal arrays with the same variable is to use the parent array you already have, d.

Arnav Aggarwal
  • 769
  • 5
  • 7