JavaScript JavaScript. How to Compare Input Arrays javascript compare input value,javascript compare input date,javascript compare

I'm stuck with this problem for 3 days now... Someone please help me.

Challenge 5
Construct a function intersection that compares input arrays and returns a new array with elements found in all of the inputs.

function intersection(arrayOfArrays) {

}

console.log(intersection([[5, 10, 15, 20], [15, 88, 1, 5, 7], [1, 10, 15, 5, 20]]));

// should log: [5, 15]
Answer:1

You could reduce the array by filtering with just checking if the other array contains the value.

This works for arrays with unique values.

Array#reduce:

If no initialValue is provided, then accumulator will be equal to the first value in the array, and currentValue will be equal to the second.

The callback

a.filter(v => b.includes(v))

filters array a. If the array b includes the value of a, then this value v is included in the accumulator for the next iteration or as final result.

     accumulator            currentValue           new accumulator
          a                       b                    result
--------------------    --------------------    --------------------
[     5, 10, 15, 20]    [15, 88,  1,  5,  7]    [             5, 15]
[             5, 15]    [ 1, 10, 15,  5, 20]    [             5, 15]

function intersection(arrayOfArrays) {
    return arrayOfArrays.reduce((a, b) => a.filter(v => b.includes(v)));
}

console.log(intersection([[5, 10, 15, 20], [15, 88, 1, 5, 7], [1, 10, 15, 5, 20]]));
Answer:2

Reduce the arrays to a Map of counts, with the value as key. Spread the Map to entries. Use Array.filter() on the Map's entries to remove all entries, which value is not equal to the arrayOfArrays lenth. Extract the original number from the entries using Array.map():

function intersection(arrayOfArrays) {
  return [...arrayOfArrays.reduce((r, s) => {
    s.forEach((n) => r.set(n, (r.get(n) || 0) + 1));
    
    return r;
  }, new Map())]
  .filter(([k, v]) => v === arrayOfArrays.length)
  .map(([k]) => k);
}

console.log(intersection([[5, 10, 15, 20], [15, 88, 1, 5, 7], [1, 10, 15, 5, 20]]));
Answer:3

First try to find out the intersection of two arrays which is the base problem. Then try to build up for variable number of arrays passed as arguments for intersection. You can use reduce() for doing that.

function intersectionOfTwoArrays(arr1, arr2)
{
   return arr1.filter(x => arr2.some(y => y === x)); 
}


function intersection(...arrayOfArrays)
{
	return arrayOfArrays
	       .reduce((a, b) => intersectionOfTwoArrays(a, b));
}

intersection(
[5, 10, 15, 20], 
[15, 88, 1, 5, 7], 
[1, 10, 15, 5, 20]
);
Answer:4

You can go through the first array in the array of arrays and check which of its value is present in all the other arrays.

Here is an example:

function intersection(input) {
    let firstArray = input[0];
    let restOfArrays = input.splice(1);
    return firstArray.filter(v => restOfArrays.every(arr => arr.includes(v)));    
}

const input = [[5, 10, 15, 20], [15, 88, 1, 5, 7], [1, 10, 15, 5, 20]];
const result = intersection(input);

console.log(result);
Answer:5

Works with even if there is duplicate in same array.. like in my example added 5 twice in arrayEle[1];

var arrayEle = [[5, 10, 15, 20], [15, 88, 1, 5, 5], [1, 10, 15, 5, 20]]
var startIndex = 1;
var newArray = [];
for (var x = 0; x < arrayEle[0].length; x++) {
  var temVal = 1;
  var value;
  for (var y = 1; y < arrayEle.length; y++) {
    for (var z = 0; z < arrayEle[y].length; z++) {
      if (arrayEle[y][z] == arrayEle[0][x]) {
        temVal++;
        value = arrayEle[y][z];
        break;
      }
    }
  }
  if (temVal == arrayEle.length) {
    newArray.push(value);
    console.log(value);
  }
}
console.log(newArray);


//log: [5, 15]
Answer:6

I want to develop a browser extension for google chrome and for firefox, which will have shared code. Is there a way, that both extensions (the firefox version and the chrome version) will use the ...

I want to develop a browser extension for google chrome and for firefox, which will have shared code. Is there a way, that both extensions (the firefox version and the chrome version) will use the ...

I have multiple arrays of promises Each array is put inside a Promise.all() The then() of each Promise.all() adds data to a tempObject I need to set the tempObject to state after then() of all ...

I have multiple arrays of promises Each array is put inside a Promise.all() The then() of each Promise.all() adds data to a tempObject I need to set the tempObject to state after then() of all ...

Is there a way to do both ( toLowerCase and includes ) in one go like below? let x = this.state.outreach.arival.toLowerCase(includes("likely"))

Is there a way to do both ( toLowerCase and includes ) in one go like below? let x = this.state.outreach.arival.toLowerCase(includes("likely"))

  1. string lowercase contains
  2. string contains lowercase java
  3. string contains lowercase c#
  4. string contains lowercase javascript

I would like to run a website developed with node.js in local. I already installed node.js but when I lauch a .js file on my terminal, nothing happen ( $ node file.js ) Also, I guess I have to ...

I would like to run a website developed with node.js in local. I already installed node.js but when I lauch a .js file on my terminal, nothing happen ( $ node file.js ) Also, I guess I have to ...

  1. website developed with
  2. website developed with django
  3. website developed with angular
  4. website developed with php
  5. website developed with node.js
  6. website developed in python
  7. website developed by footer
  8. website developed in which language
  9. website developed in react
  10. website developed in which technology
  11. website developed in wordpress
  12. website developed in angularjs
  13. website developed in java
  14. website developed in angular 2
  15. website developed in magento
  16. website developed in asp.net
  17. website developed in vuejs
  18. website developed in ruby on rails
  19. website developed in html
  20. website developed by tcs