Hello:) how may I find in two (or more) arrays duplicates and show it? I want to use filter, is it good idea?
`
let monday = ["task1", "task2", "task3", "task4", "taks5", "task6", "taks7"];
let tuesday = ["task2", "task3", "task4", "task5", "taks6"];
let wendesday = ["task7", "task8", "task9", "task10", "taks11", "task12"];
const takePastList = monday
.concat(tuesday)
.join(" ")
.filter((task, index) => index !== monday.indexOf(task));
` concat works but filter no;)
Regards and thank You
Find how to fix it and understand how to use filter or other method to find duplicate when I work on many arrays with many methods.
CodePudding user response:
Here's your solution:
let monday = ["task1", "task2", "task3", "task4", "task5", "task6", "task7"];
let tuesday = ["task2", "task3", "task4", "task5", "task6"];
let wednesday = ["task7", "task8", "task9", "task10", "task11", "task12"];
const takePastList = monday
.concat(tuesday)
.concat(wednesday)
.filter(function(v, _, a) {
return a.indexOf(v) === a.lastIndexOf(v);
});
console.log(takePastList)
CodePudding user response:
May be something like this might suits your needs
let monday = ["task1", "task2", "task3", "task4", "taks5", "task6", "taks7"];
let tuesday = ["task2", "task3", "task4", "task5", "taks6"];
let wendesday = ["task7", "task8", "task9", "task10", "taks11", "task12"];
//solution
let allTasks = monday.concat(tuesday, wendesday);
let duplicates = allTasks.filter((item, index) => allTasks.indexOf(item) !== index);
console.log(duplicates);
CodePudding user response:
If you are trying to keep and display duplicated values, you were on the right lines but made a mistake using .join(" ").
.join()
is an array method
that returns a string
of the array elements joined together into a string
variable.
You are then trying to apply your .filter
(an array method
) to a string
.
If you delete the .join(" ")
step, your routine works to return an array holding the values that were present in both the two .concat
enated arrays.
The resulting array can be processed again to add the third array as demonstrated in this two-step snippet (based on your .map
function).
let monday = ["task1", "task2", "task3", "task4", "taks5", "task6", "taks7"];
let tuesday = ["task2", "task3", "task4", "task5", "taks6"];
let wednesday = ["task7", "task8", "task9", "task10", "taks11", "task12"];
const takePastList = monday
.concat(tuesday)
.filter((task, index) => index !== monday.indexOf(task));
const nextAr = takePastList.concat(wednesday).filter((task, index) => index !== takePastList.indexOf(task));
console.log(takePastList);
(Wednesday spelling corrected)
In practice you might want to build a function that can be called repeatedly, or recursively, to include further arrays for comparison. There is a caveat - if there was an element in the first array that is not present in the second, it will not be in the output of the first cycle and so will not be detected as a duplicate if the third array is compared to the output of the first two procedure. It depends what you need whether this is a problem.
Basically, your .map
function was fine, but it should not have been applied to a string
CodePudding user response:
Here's another solution if you are looking for duplicates.
let monday = ["task1", "task2", "task3", "task4", "task5", "task6", "task7"];
let tuesday = ["task2", "task3", "task4", "task5", "task6"];
let wendesday = ["task7", "task8", "task9", "task10", "task11", "task12"];
const takePastList = monday
.concat(tuesday);
const duplicates = takePastList.filter((item, index) => index !== takePastList.indexOf(item));
console.log(duplicates)