要求如图:
为相同category的项中加入相同的color参数:
var arr = [
{ id: 1, label: "苹果", category: "水果" },
{ id: 2, label: "猴子", category: "动物" },
{ id: 3, label: "大象", category: "动物" },
{ id: 4, label: "火龙果", category: "水果" },
{ id: 5, label: "芭蕉树", category: "植物" },
]
categoryArray = [
{ category: "水果", count: 2 },
{ category: "动物", count: 2 },
{ category: "植物", count: 1 },
]
var color = ["pink", "red", "orange", "blue"]
var cg = categoryArray.map((item) => {
return item.category
})
for (var i = 0; i < cg.length; i++) {
for (var j = 0; j < arr.length; j++) {
if (cg[i] == arr[j].category) {
arr[j].color = { background: color[i] }
}
}
}
console.log(arr)