I have a use case wherein I have to compare two elements of an array and display the similar elements along with the index. Suppose, I have two arrays say,
var array1 = ["One", "Two", "Three"];
var array2 = ["Four", "One", "Five"];
I want the output to be as follows:
array1
One - 1
array2
One - 2
I have written the below code snippet to find out the similar elements in both the arrays but unable to proceed on the above.
const filteredArray = array1.filter(value => array2.includes(value));
Please help.
CodePudding user response:
- Using
Array#reduce
, iterate overarray1
while updating the resulting list of common items having the value, index inarray1
, and index inarray2
- In each iteration, using
Array#findIndex
, get the index of the current value inarray2
, if it exists, push a new item to the resulting array.
const
array1 = ["One", "Two", "Three"],
array2 = ["Four", "One", "Five"];
const filteredArray = array1.reduce((commonItems, value, index1) => {
const index2 = array2.findIndex(e => e === value);
if(index2 >= 0) {
commonItems.push({ value, index1, index2 });
}
return commonItems;
}, []);
console.log(filteredArray);
CodePudding user response:
Solution with basic for
loops:
var array1 = ["One", "Two", "Five"];
var array2 = ["Four", "One", "Five"];
for (let i = 0; i < array1.length; i ) {
for (let j = 0; j < array2.length; j ) {
if (array1[i] == array2[j]) {
console.log("array1");
console.log(array1[i] " - " (i 1));
console.log("array2");
console.log(array2[j] " - " (j 1));
}
}
}
Output :
array1
One - 1
array2
One - 2
array1
Five - 3
array2
Five - 3
CodePudding user response:
Clarification : How for array2 it should be One - 2
? As One
is in 0th index in array1
As per my understanding, You can simply achieve this by using Array.forEach()
along with the Array.indexOf()
method.
Live Demo :
var array1 = ["One", "Two", "Three"];
var array2 = ["Four", "One", "Five"];
array1.forEach(item => {
if (array2.indexOf(item) !== -1) {
console.log(item, array2.indexOf(item));
}
});
array2.forEach(item => {
if (array1.indexOf(item) !== -1) {
console.log(item, array1.indexOf(item));
}
});
CodePudding user response:
What I would do if needed the index is
- Check 2 arrays to see if they contain the same element
- Check the indexOf() on each array for that value.
// Set values
const array1 = ["one", "two", "three"];
const array2 = ["three", "five", "four"];
// Find duplicates
const duplicates = array1.filter((item) => array2.includes(item));
const findDiffIndexs = (values, arr1, arr2 ) => {
const indexes = [];
values.forEach((value) => {
const index1 = array1.indexOf(value);
const index2 = array2.indexOf(value);
const label = `Value: '${value}' is found in array1[${index1}] & array2[${index2}]`;
indexes.push(label);
});
return indexes;
}
if (duplicates) console.log(findDiffIndexs(duplicates, array1, array2));
CodePudding user response:
You can do it in this way & store it in an object
var array1 = ["One", "Two", "Three"];
var array2 = ["Four", "One", "Five"];
let obj={ array1:[],array2:[] }
array1.filter((value,i) => {
if(array2.indexOf(value) !== -1){
console.log('value',value, array2.indexOf(value))
obj["array1"].push(`value - ${i 1}`)
obj["array2"].push(`value - ${array2.indexOf(value) 1}`)
}
});
console.log(obj)
Output
{ array1: [ 'value - 1' ], array2: [ 'value - 2' ] }