我试图实现一个逻辑,我有一个数组[3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6]
。我想找到所有重复的元素我想把这些重复的元素存储到一个新的数组中。我很努力,但没有找到解决办法。
如果有人能写出简单的代码并解释代码的作用,那就太好了。
感谢首先你需要了解一些基础知识。
阵列图- MDN资料
数组映射创建一个新数组,不改变当前数组。
//So if we do:
[3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6].map((element, index, array) => { return 0; } );
//We get
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Array.indexOf(元素)MDN信息
返回数组中元素的索引,未找到-1,从0开始。
//So if we do:
[3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6].map( (element, index, array) => { return array.indexOf(element); } );
// we get
// [0, 1, 2, 3, 3, 5, 3, 7, 2, 7, 1, 3, 12, 7, 1, 15, 16, 7, 18, 5, 1, 3, 22, 0, 12]
//so if we do use the second parameter of map in combination with indexOf
[3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6].map( (element, index, array) => { return array.indexOf(element) === index; } );
// we get
// [true, true, true, true, false, true, false, true, false, false, false, false, true, false, false, true, true, false, true, false, false, false, true, false, false]
总结一下:
[3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6].map( (element, index, array) => {
if ( array.indexOf(element) !== index ) {
return element;
} else {
return undefined;
}
});
// will give us
// [undefined, undefined, undefined, undefined, 5, undefined, 5, undefined, 63, 2, 4, 5, undefined, 2, 4, undefined, undefined, 2, undefined, 1, 4, 5, undefined, 3, 6]
Array Filter Information on MDN
// so now we have our array of elements that are duplicated with undefined values in it, now we filter.
[undefined, undefined, undefined, undefined, 5, undefined, 5, undefined, 63, 2, 4, 5, undefined, 2, 4, undefined, undefined, 2, undefined, 1, 4, 5, undefined, 3, 6].filter( (element) => {
return element !== undefined;
});
//result
[5, 5, 63, 2, 4, 5, 2, 4, 2, 1, 4, 5, 3, 6]
这是我能给出的最基本的解释,不使用条件操作符,存储变量等。如果你在编程方面更高级一点,你可以编写更短的代码,称为1 liner。但我希望这能给你继续下去的灵感。但你必须先了解最基本的,这意味着学习,阅读,多学习和大量练习。
如果你只需要数组中的重复项,你可以使用:
var arry = [3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6];
var rep_arry = [];
for (const [key,val] of Object.entries(arry)) {
for (const [key1,val1] of Object.entries(arry)) {
if(val == val1 && !rep_arry.includes(val) && key != key1){
rep_arry.push(val);
}
}
}
console.log(rep_arry);
它返回这个
[3, 4, 63, 5, 1, 2, 6]
上面的代码使用两个for循环来检查每个单独的元素,每个元素使用两个for循环。在push之前,我设置了3个条件,1用于检查相等,2用于已经存在检查项目是否可以进入新数组,第三个用于防止使用键检查其自己的元素。它也适用于大量的项目。参见代码片段:
<html>
<script>
var arry = [3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6];
var rep_arry = [];
for (const [key,val] of Object.entries(arry)) {
for (const [key1,val1] of Object.entries(arry)) {
if(val == val1 && !rep_arry.includes(val) && key != key1){
rep_arry.push(val);
}
}
}
console.log(rep_arry);
</script>
</html>
我们可以通过保持唯一元素列表来解决这个问题.如果我们保留唯一元素的列表,那么收集重复元素就很容易了。
这可能不是提高效率的最佳方案,但它解决了当前的问题。我注释了代码,这样也许能帮助你理解发生了什么。
要解决这类问题,最好先学习算法。
const arr = [3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6];
let uniqueElmts = []; // here we gonna store unique elements
let duplicates = []; // here we gonna store duplicate elements
for(let i = 0; i < arr.length; i++){
if(uniqueElmts.includes(arr[i])){ // if 'uniqueElmts' already contains arr[i], then arr[i] is a duplicate
duplicates.push(arr[i]); // push arr[i] element to 'duplicates' array, because it's duplicate
}
else {
uniqueElmts.push(arr[i]); // if 'uniqueElmts' doesn't contain arr[i] then it's unique element so we push arr[i] to 'uniqueElmts'
}
}
console.log(duplicates); // [ 5, 5, 63, 2, 4, 5, 2, 4, 2, 1, 4, 5, 3, 6 ]