我有这样的主数组,虽然它不是完整的数组,但这是数组的格式
newarr: Array(1)
0: Array(239)
[0 … 99]
0: i {transformMatrix: null, fill: "#FFFFFF", dirty: false, stroke: "#000000", strokeWidth: 0.172, …}
1: i {transformMatrix: null, fill: "#000000", dirty: false, stroke: "#000000", strokeWidth: 0.172, …}
2: i {transformMatrix: null, fill: "#FFFFFF", dirty: false, stroke: "#000000", strokeWidth: 0.172, …}
3: i {transformMatrix: null, fill: "#0000FF", dirty: false, stroke: "#000000", strokeWidth: 0.172, …}
4: i {transformMatrix: null, fill: "#FFFFFF", dirty: false, stroke: "#000000", strokeWidth: 0.172, …}
5: i {transformMatrix: null, fill: "#FFFFFF", dirty: false, stroke: "#000000", strokeWidth: 0.172, …}
第二个数组是我要保存第一个数组中所有唯一颜色的地方,它看起来像这样
uniqueColors: Array(1)
0: "#FFFFFF"
1: "#000000"
2: "#0000FF"
length: 1
__proto__: Array(0)
现在我想使用send数组元素查找搜索数组1并将相同的颜色索引以组形式保存在第三个数组中。
答案 0 :(得分:2)
此代码同时执行两个步骤:
arr1 = [{
transformMatrix: null,
fill: "#FFFFFF",
dirty: false,
stroke: "#000000",
strokeWidth: 0.172
},
{
transformMatrix: null,
fill: "#000000",
dirty: false,
stroke: "#000000",
strokeWidth: 0.172
},
{
transformMatrix: null,
fill: "#FFFFFF",
dirty: false,
stroke: "#000000",
strokeWidth: 0.172
},
{
transformMatrix: null,
fill: "#0000FF",
dirty: false,
stroke: "#000000",
strokeWidth: 0.172
},
{
transformMatrix: null,
fill: "#FFFFFF",
dirty: false,
stroke: "#000000",
strokeWidth: 0.172
},
{
transformMatrix: null,
fill: "#FFFFFF",
dirty: false,
stroke: "#000000",
strokeWidth: 0.172
}
]
dupe = new Set()
arr2 = arr1.filter(({fill}) => {
if (dupe.has(fill)) {
return false
} else {
dupe.add(fill)
return true
}
})
obj = arr2.reduce((acc1, {fill:f1}) => {
acc1[f1] = arr1.reduce((acc2, {fill:f2}, i) => f1 === f2 ? [...acc2, i] : acc2, [])
return acc1
}, {})
console.log(obj)
arr1
中的所有重复项,并将其余部分返回arr2
,并使用一组注释重复项。