要求如圖:
爲相同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)