0

I have an array where each row is an array that has 2 values, S/N and Description.

 array1 = [["xx9","Big Car"],["xx1","Small Car"],["xx9","Big Car"],["xx9"," Big Car"]];

As you can see there are duplicates. So I wrote code that creates a count array that holds the key-value pairs and counts each item. Length of count is 1.

count = [xx1: 1
         xx9: 3] 

So now I want to combine the two arrays into one new one that includes the quantity, in this example the final output would be as shown below. Note that I have to also remove the duplicates in array1. Order doesn't matter.

 final_array = [["1","xx1","Small Car"],["3",xx9","Big Car"]];

Here is my JS code so far but basically the .forEach loops won't work over the count array. Can anyone help me so that this code works on the key-value array.

JS/Jquery:

//Pull the Cart Data & Orangize
var savedList = JSON.parse(localStorage.getItem("partList"));
        console.log(savedList);
        determineQuantity(savedList); //Count quantity of each item

//Count the Quantity of each item in saved list & create key-value pairs
function determineQuantity(savedList){
    var newList = [];
    var count = [];
    for (var i=0, j = savedList.length; i<j; i++){
        count[savedList[i][0]] = (count[savedList[i][0]] || 0) + 1;
    };
        console.log(count);
    //Combine Quantity Array with SavedList Array
    count.forEach(function(item,index){ 
                    console.log("index = " + index + "  item = " + item);
          savedList.forEach(function(row){
                if ($.inArray(item,row == 0)){ //if found in this row at index 0
                     var new_part = [count[index],row[0],row[1]];
                            console.log("new_part = " + new_part);
                     newList.push(new_part);
                };  
        });
    });
    console.log(newList);
};

1 Answer 1

1

how about this.

var test = function(){
    var array = [["xx9","Big Car"],["xx1","Small Car"],["xx9","Big Car"],["xx9"," Big Car"]];
    var count = {xx1: 1, xx9: 3};
    var map = {};
    array.forEach(function(item){map[item[0]] = item[1]});
    var newArray = [];
    for(var key in count){
        if(!map[key])
            continue;
        newArray.push([count[key], key, map[key]]);
    }
    console.log(newArray);
}

first of all, you count is a object, not a array. then we dont need forEach

Sign up to request clarification or add additional context in comments.

5 Comments

can you explain this map[item[0]] = item[1] ... why do we equate it to item[1] ?
we just create a object called map. that hold all key/pair values. something like {"xx9" : "BigCar", "xx1" : "Small car"}. map["xx9"] = "Big Car"; map["xx1"] = "Small Car"; this is what we are doing at forEach
Ahh I see now. item[0] is xx9 and item [1] is BigCar My next question is can you explain !map[key] and continue?
suppose in case if you array doest not contain ["xx1","Small Car"], but you count object contain xx1:1. what we need to that situation? here just ignore that kind of case.
Okay so everything makes sense now. Since count is not an object you converted my array into a key-value pair and compared it with count and then combined them into a new array. So I tested it out and it worked thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.