print是javascript中数组的副本



如果发现重复,则从第一个索引处的嵌套数组中查找重复值显示第一行是重复的,如果不是,则应表示此行不是重复的

var array = [
["fruits", "Apple", "vegetable", "Potato"],
["fruits", "Mango", "vegetable", "Tomoto"],
["fruits", "Apple", "vegetable", "Carrot"]
];
const counter = {}
array.flat().forEach(i => counter[i] ? counter[i]++ : counter[i] = 1);
const dupes =Object.entries(counter)
.filter(([k, v]) => v > 1)
.map(([k,v])=> k);

function myFunction() {
text = array.map(i => !dupes.some(d=> i.includes(d)));
document.getElementById("demo").innerHTML = text;
}

尝试了该代码并表示所有都是false,应该首先检查所有数组的索引并打印值。

在这种情况下,它应该说第一行是重复的,第二行不重复,第三行是重复,因为苹果存在于第一行和第三行。它应该只检查第一个索引。

感谢

怎么样?

对于数组中的每个集合,都会检查第一个索引,并记录值和集合索引。然后,在处理完所有集合之后,将根据记录的索引创建一个结果数组。

var array = [
["fruits", "Apple", "vegetable", "Potato"],
["fruits", "Mango", "vegetable", "Tomoto"],
["fruits", "Apple", "vegetable", "Carrot"]
];
// Loop through sets
// Check index=1, check obj for key
//  If doesn't exist, create it with one element array containing index
//  If does exist, add duplicate index to array
function findFirstIndexDupes(arr) {
var dupesObj = {};
for (var i = 0; i < arr.length; i++)
if (dupesObj[arr[i][1]] == undefined)
dupesObj[arr[i][1]] = [i];
else
dupesObj[arr[i][1]].push(i);
var output = Array(arr.length).fill(true);
for (var dupe in dupesObj)
if (dupesObj[dupe].length > 1)
dupesObj[dupe].forEach(e => output[e] = false);
return output;
}
console.log(findFirstIndexDupes(array));

最新更新