Javascript比较和组合新数组中的项目

时间:2014-03-01 18:59:25

标签: javascript arrays

我正在尝试使用数组列表并将类似对象组合成一个新数组,但我仍然没有达到我想要做的事情。注意:我正在使用jQuery $.each()以及其他可能使它“慢”的东西,但我只想让逻辑首先工作。

所以,简短的故事:我有用户可以选择的项目,当他们选择一个项目时,我把它放入一个名为materials的数组列表中。此数组列表包含用户选择的所有内容以及项目的所有子项。这里和示例:如果用户选择“Wood Barricade”和“Sleeping Bag”,它将添加到materials数组并输出如下:

["woodBarricade|1", "wood|30", "sleepingBag|1", "cloth|15"];

现在,如果用户添加“Wood Planks”,则数组将变为:

["woodBarricade|1", "wood|30", "sleepingBag|1", "cloth|15", "woodPlanks|1", "wood|10"];

我现在要做的就是选择相似的内容。在上面的例子中,它应该结合两个“木”项,新数组应该说:

["woodBarricade|1", "wood|40", "sleepingBag|1", "cloth|15", "woodPlanks|1"];

这是我到目前为止所拥有的:

function calculateMaterials(){ //Start the function
    var conMaterialList = []; //Empty the list every time.
    $.each(materials, function(a){ //for each material in the list
        var materialName = materials[a].split("|")[0]; //Get it's name
        var materialCount = parseInt(materials[a].split("|")[1]);  //And the quantity
        if(conMaterialList == ""){  //Then if the conMaterialList is empty
            conMaterialList.push(materialName+"|"+materialCount); //Add the first material
        } else {  // If the conMaterialList is NOT empty
            $.each(conMaterialList, function(b){ //iterate through the list
                var materialNameComp = conMaterialList[b].split("|")[0]; //Get the name
                var materialCountComp = parseInt(conMaterialList[b].split("|")[1]); //Get the quantity
                if(materialName == materialNameComp){ // If the item from 'materials' matches an item from 'conMaterialList'
                    conMaterialList.splice(b, 1, materialNameComp +"|"+ parseInt(materialCount + materialCountComp)); //combine that material
                } else {  // If it does not match
                    conMaterialList.push(materialName +"|"+ materialCount); //push that material into the array
                }
            });
        }
    });
    console.log(conMaterialList); //Show me the conMaterialList
}

这个ALMOST有效。如果我尝试上面的例子(选择Wood路障,睡袋和木板),我会把它作为输出:

["woodBarricade|1", "wood|40", "sleepingBag|1", "sleepingBag|1", "cloth|15", "cloth|15", "cloth|15", "cloth|15", "woodPlanks|1", "woodPlanks|1", "woodPlanks|1", "woodPlanks|1", "woodPlanks|1", "woodPlanks|1", "woodPlanks|1", "woodPlanks|1", "wood|10", "wood|10", "wood|10", "wood|10", "wood|10", "wood|10", "wood|10", "wood|10", "wood|10", "wood|10", "wood|10", "wood|10", "wood|10", "wood|10", "wood|10"]

如您所见,第一个“木头”的数量为40.这在技术上确实将两个木桩加在一起。但此后发生的事情对我来说是一个谜。

我已经被告知我这是以最慢的方式做这件事,但我希望逻辑在我加速之前先工作。

关于如何组合项目并制作单一,简洁列表的任何想法?

1 个答案:

答案 0 :(得分:0)

我试图让代码接近你的逻辑,但使用object而不是array。

function calculateMaterials(){ //Start the function
    var conMaterialList = {}; //Start with the blank object.
    $.each(materials, function(materialName, quantity){ //for each material in the obj
        if (materialName in conMaterialList) { // Material exists
            comMaterialList[materialName] += quantity;  // add quantity
        }
        else {  // Material not there
            comMaterialList[materialName] = quantity;   // add material with quantity
        }
     });
    console.log(conMaterialList); //Show me the conMaterialList
}