我有一个对象数组(在Python中称为字典,但在这种情况下我使用Javascript)。我的目标是将不同的对象分组。我的第一次尝试是基于库存值创建两个数组。然后对于每个数组,我想基于目标值创建三个不同的数组。
我认为我失去了逻辑,因为第二个分组(目标)不起作用。
我认为我失去了逻辑,因为第二个分组(目标)不起作用。
var inventory = [
{ID: "K111", Objective: "One", Inventory: "Second" },
{ID: "K112", Objective: "Two", Inventory: "Second" },
{ID: "K113", Objective: "One", Inventory: "Second" },
{ID: "K114", Objective: "Three", Inventory: "Second" },
{ID: "K132", Objective: "One", Inventory: "First" }
];
var OBJECTIVE = ["One", "Two", "Three"];
var INVENTORY = ["Second", "First"];
//Create arrays per Inventory (2 possible values)
for (var i = 0; i < INVENTORY.length; i++) {
var variable = inventory.filter(function(el) {
return el.Inventory == INVENTORY[i];
});
window['arr'+i] = variable;
document.getElementById("arr"+i).innerHTML =JSON.stringify(variable);
}
//Create arrays per Objective for each array created above
for (var i = 0; i < OBJECTIVE.length; i++) {
j = i + 2;
var variable = arr0.filter(function(el) {
return el.Objective == OBJECTIVE[i];
});
window['arr'+j] = JSON.stringify(variable);
document.getElementById("arr"+j).innerHTML =JSON.stringify(variable);
}
for (var i = 0; i < OBJECTIVE.length; i++) {
var variable = arr1.filter(function(el) {
return el.Objective == OBJECTIVE[i];
});
window['arr'+j] = JSON.stringify(variable);
document.getElementById("arr"+j).innerHTML =JSON.stringify(variable);
}
<div style="background:yellow;" id="arr0"></div>
<div style="background:green;" id="arr1"></div>
<div style="background:grey;" id="arr2"></div>
<div style="background:blue; color:white;" id="arr3"></div>
<div style="background:red; color:white;" id="arr4"></div>
<div style="background:black; color:white;" id="arr5"></div>
<div style="background:orange;" id="arr6"></div>