简体   繁体   中英

Finding intersection in multiple arrays

I am trying to find the intersecting values in multiple arrays, that are within an object as follows:

object = {
  filterA: ["1","2","3","4"],
  filterB: ["2","5","6","7"],
  filterN: ["2","4","7"]
}

The object can hold multiple arrays and the name of the keys can vary. In the described object I need only "2" to be returned.

I have tried to build on this answer: Multiple array intersection in javascript but I could not figure it out, as it uses static variables (a,b,c) for the example. If there is a way to do that with lodash, it will be appreciated, but vanilla javascript in ES 5 will do as well!

You could get the values and take a Set and filter with Set#has .

 var object = { filterA: ["1", "2", "3", "4"], filterB: ["2", "5", "6", "7"], filterN: ["2", "4", "7"] }, result = Object .values(object) .reduce((a, b) => b.filter(Set.prototype.has, new Set(a))); console.log(result);

I have a simple one-liner approach:

const res = Object.values(object).reduce((a, b) => a.filter(i => b.includes(i)))

But since you want it in ES5 (ECMAScript 2009) version, that means no arrow functions, no includes(), and no usage of Set objects.

const res = Object.values(object).reduce(function (a, b) { 
  return a.filter(function(i) {
    //return b.includes(i);
    return b.indexOf(i) >= 0;
  });
});

console.log(res);

Iterate over first property value(array) and filter out by comparing with remaining property value.

// get property values(arraya as array)
let arr = Object.values(object);


let res = arr[0]
  // iterate over first array
  // check value present in remaining object values(arrays)
  .filter(v => arr.slice(1).every(a => a.includes(v)));

 let object = { filterA: ["1", "2", "3", "4"], filterB: ["2", "5", "6", "7"], filterN: ["2", "4", "7"] }; let arr = Object.values(object); let res = arr[0].filter(v => arr.slice(1).every(a => a.includes(v))); console.log(res)

reduce will do the work for you. Check out my code below.

 var object = { filterA: ["1","2","3","4"], filterB: ["2","5","6","7"], filterN: ["2","4","7"] } var res = Object.values(object).reduce(function (acc, array) { return _.intersection(acc, array) }) console.log(res)
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>

object = {
  filterA: ["1","2","3","4"],
  filterB: ["2","5","6","7"],
  filterN: ["2","4","7"]
}

let arr = Object.values(object);

let res = arr[0].filter(v => arr.slice(1).every(a => a.includes(v)));


console.log(res)

Lodash's _.intersection() can handle multiple arrays. You can create a function with _.flow() that gets an array of arrays from the object with _.values() , and computes the intersection by using _.intersection() with _.spread() .

 const { flow, values, spread, intersection } = _ const fn = flow( values, // get the arrays spread(_.intersection) // spread into intersection ) const object = { filterA: ["1","2","3","4"], filterB: ["2","5","6","7"], filterN: ["2","4","7"] } const result = fn(object) console.log(result)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

let arrayOfComparisonArrays = [[1,2,3,4,5], [5,3], [1,3,4,5], [5,1,3]]
let intersectionArray

intersectionArray = arrayOfComparisonArrays.reduce((lastArray, nextArray)=>{
    let intersection = lastArray.filter(x => nextArray.includes(x))
    return intersection
})

//intersectionArray: [ 3, 5 ] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM