简体   繁体   English

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

[英]Javascript Comparing and Combining items in a new array

I'm trying to take an array list and combine like objects into a new array, but I keep coming up short of what I'm trying to do. 我正在尝试获取一个数组列表,并将类似的对象组合到一个新的数组中,但是我一直没有做到。 Note: I am using jQuery $.each() among other things that might make this 'slower' but I just want the logic to work first. 注意:我正在使用jQuery $.each()等等,这可能会使它“变慢”,但我只是希望逻辑首先起作用。

So, short story: I have items that a user can select, When they select an item, I put it into an array list called materials . 简而言之:我有一些用户可以选择的项目,当他们选择一个项目时,我将其放入一个称为materials的数组列表中。 This array list houses everything the user has selected and all of the item's sub-items. 该数组列表包含用户选择的所有内容以及该项目的所有子项目。 here's and example: If the user selects "Wood Barricade" and "Sleeping Bag" it will add to the materials array and output like so: 这是示例:如果用户选择“ Wood Barricade”和“ Sleeping Bag”,它将添加到materials阵列并输出,如下所示:

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

Now, if the user adds in "Wood Planks", the array becomes: 现在,如果用户添加“木板”,则数组将变为:

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

What I am trying to do now is combine like selections. 我现在想做的就是像选择一样组合。 In the example above, It should combine the two "wood" items and the new array should say: 在上面的示例中,它应将两个“ wood”项组合在一起,新数组应显示为:

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

Here's what I have so far: 这是我到目前为止的内容:

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
}

This ALMOST works. 此ALMOST有效。 If I were to try this with the example above (Select Wood barricade, sleeping bag and wood plank) I would get this as the output: 如果我要使用上面的示例(选择木路障,睡袋和木板)尝试此操作,则将其作为输出:

["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"]

As you can see, the first "wood" has a quantity of 40. This technically did Add the two wood piles together. 如您所见,第一个“木头”的数量为40。从技术上讲,这确实将两个木头堆加在一起。 But what happened after that is a mystery to me at the moment. 但是此后发生的事情对我而言目前是个谜。

I've already been told that I'm doing this the slowest way possible, but I would like the logic to work first before I speed it up. 我已经被告知我正在以最慢的方式执行此操作,但是我希望在加速之前先执行逻辑。

Any ideas on how to combine items and make a single, succinct list? 关于如何组合项目并制作单个简洁列表的任何想法?

I tried to keep the code close to your logic, but using object instead of 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM