0

I have a two dimensional array:

[[bourbon, 2], [bourbon, 1],[scotch, 2]]

I would like to end up with a new array that consolidates the duplicates so that the array would become

 [[bourbon, 3], [scotch, 2]]

Here is how I create the array:

    for(var i=0; i< data.length; i++){
         var typeW = String(data[i].doc.type);
         var valueAsNumber = parseInt(data[i].doc.bottle);
         typeOfWhiskey[j,i] = [typeW,valueAsNumber];
         j++;
    }   

I have tried to check for unique values with:if(typeOfWhiskey.indexOf(typeW ) > -1){

However I am currently stuck. In this example 'typeW' is the string value for 'bourbon', or 'scotch' for example, where as 'valueAsNumber' would be 2 or 1 based on the example provided. I would like to not have to create another for look and iterate through the whole array again because I feel that would be inefficient. I think I am close but am not sure how to proceed. Thanks

3
  • 1
    What do you think typeOfWhiskey[j,i] is doing? Commented May 11, 2015 at 22:43
  • @FelixKling it is allowing me to construct the array. j and i increment as I more through the array where I get the values causing typeOfWhiskey to be built. Commented May 11, 2015 at 22:45
  • Mmh.... typeOfWhiskey[j,i] is eqivalent to typeOfWhiskey[i]. j,i is the comma operator at work, so the whole expression evaluates to i. You might want typeOfWhiskey[j][i] Commented May 11, 2015 at 22:46

2 Answers 2

1

This will work:

var source = [['bourbon', 2], ['bourbon', 1],['scotch', 2]];
var hash = {};
var consolidated = [];

source.forEach(function(item) {
     hash[item[0]] = (hash[item[0]] || 0) + item[1];
});

Object.keys(hash).forEach(function(key) {
     consolidated.push([key, hash[key]]);
});

jsFiddle

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

Comments

1

Create a copy of initial array.

I did something similar recently, this might help:

// Create copy to delete dups from
$copy = $sowArray; 
$sharedDescriptions = array();

for( $i=0; $i<count($sowArray); $i++ ) {
    if ( in_array( $sowArray[$i][$description], $sharedDescriptions ) ) {
        unset($copy[$i]);
    }
    else {
        $sharedDescriptions[] = $sowArray[$i][$description];
    }
}
$sharedDescriptions = array_values($sharedDescriptions);
$copy = array_values($copy);

// Update quantities of duplicate items
for( $i=0; $i<count($copy); $i++ ) {
    $product = $copy[$i][$description];
    $qty = 0;
    if (in_array($product, $sharedDescriptions)) {
        foreach ($sowArray as $sowRow) {
            if ($sowRow[$description] === $product){
                $qty += $sowRow[$quantity];
               }
            }
        $copy[$i][$quantity] = $qty;
        } 
    }

Comments

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.