创建一个新的数组,一起添加重复的值

时间:2021-07-21 20:23:28

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){

我试图用以下方法检查唯一值: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

但是我现在卡住了。在这个例子中,'typeW'是'bourbon'的字符串值,或者例如'scotch',其中'valueAsNumber'将是2或1,基于提供的示例。我不想再创建另一个用于外观并再次遍历整个数组,因为我觉得这样效率很低。我想我很接近但不确定如何继续。谢谢

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

#2


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;
        } 
    }

#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

#2


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;
        } 
    }