如果另一个数组具有相同的元素,则连接数组

时间:2017-11-24 09:08:58

标签: javascript arrays underscore.js lodash

我有两个阵列

var arrayA = ["a", "a", "b", "b", "a", "c"];
var arrayB = [10, 20, 3, 2, 20, 5];

如您所见,arrayA[0], arrayA[1], arrayA[4]具有相同的元素(arrayA[2], arrayA[3]也相同)。

因此,基于上面的示例,我希望arrayB[0], arrayB[1], arrayB[4]将被总结,arrayB[2], arrayB[3]也是如此。

期望输出

arrayA = ["a", "b", "c"];
arrayB = [50, 5, 5];

如果arrayA具有基于arrayA索引的相同元素,那么可以对arrayB元素求和吗?并且有一个Lodash / Underscore功能来执行此操作吗?

8 个答案:

答案 0 :(得分:6)

您可以使用索引的对象并维护值。



var arrayA = ["a", "a", "b", "b", "a", "c"],
    arrayB = [10, 20, 3, 2, 20, 5],
    indices = Object.create(null),
    groupedA = [],
    groupedB = [];
    
arrayA.forEach(function (a, i) {
    if (!(a in indices)) {
        groupedA.push(a);
        indices[a] = groupedB.push(0) - 1;
    }
    groupedB[indices[a]] += arrayB[i];
});

console.log(groupedA);
console.log(groupedB);

.as-console-wrapper { max-height: 100% !important; top: 0; }




改变原始数组的版本。



var arrayA = ["a", "a", "b", "b", "a", "c"],
    arrayB = [10, 20, 3, 2, 20, 5],
    indices = Object.create(null),
    i = 0;

while (i < arrayA.length) {
    if (!(arrayA[i] in indices)) {
        indices[arrayA[i]] = i++;
        continue;
    }
    arrayB[indices[arrayA[i]]] += arrayB.splice(i, 1)[0];
    arrayA.splice(i, 1);
}

console.log(arrayA);
console.log(arrayB);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 1 :(得分:2)

这是使用lodash的解决方案:

[arrayA, arrayB] = _(arrayA)
    .zip(arrayB)
    .groupBy(0)
    .mapValues( grp => _.sumBy(grp,1))
    .thru(obj => [_.keys(obj), _.values(obj)])
    .value();

zip会将arrayA中的每个元素与arrayB中的相应元素相关联,例如[ ['a', 10], ['a', 20], ...]

我们然后groupBy位置0中的值给出一个类似的对象:

{
   a: ['a', 10], ['a', 20], ['a', 20'],
   b: ['b', 3] ...,
   c: ...
}

然后,在最终返回键和单独数组中的值之前,将每个键的值映射到位置1中的值的总和。

答案 2 :(得分:2)

您可以reduce将两个阵列都放入ES6 Map,然后将spread放入阵列A的keys,将values放入阵列B:

const arrayA = ["a", "a", "b", "b", "a", "c"];
const arrayB = [10, 20, 3, 2, 20, 5];

const map = arrayA.reduce((m, c, i) => m.set(c, (m.get(c) || 0) + arrayB[i]), new Map());

const arrA = [...map.keys()];
const arrB = [...map.values()];

console.log(arrA);

console.log(arrB);

答案 3 :(得分:1)

使用Array#reduce方法。

&#13;
&#13;
var arrayA = ["a", "a", "b", "b", "a", "c"];
var arrayB = [10, 20, 3, 2, 20, 5];

// reference to keep the index
var ref = {},
  // array for keeping first result
  res1 = [];


var res2 = arrayA
  // iterate over the first array
  .reduce(function(arr, v, i) {
    // check index presenet in the refernece object
    if (!(v in ref)) {
      // if not then define the index and insert  0 in the array(defining the new index)
      arr[ref[v] = arr.length] = 0;
      // push value into the array( for unique value )
      res1.push(v);
    }
    // update the element at the index
    arr[ref[v]] += arrayB[i];
    // return the array reference
    return arr;
    // initialize initial value as an empty array to keep result
  }, [])

console.log(res1, res2);
&#13;
&#13;
&#13;

答案 4 :(得分:1)

您可以计算arrayB中与arrayA中每个元素对应的所有元素的总和,将这些和存储在一个对象中,并使用Object.values来获取总和的数组。

var arrayA = ["a", "a", "b", "b", "a", "c"];
var arrayB = [10, 20, 3, 2, 20, 5];

var sum = {};

arrayA.forEach((l, index) => {
    sum[l] = (sum[l] || 0) + arrayB[index];
});

var res = Object.values(sum);

console.log(res);

使用 array.prototype.reduce 可以做得更短:

var arrayA = ["a", "a", "b", "b", "a", "c"];
var arrayB = [10, 20, 3, 2, 20, 5];

var res = Object.values(arrayA.reduce((m, l, index) => {
    m[l] = (m[l] || 0) + arrayB[index];
    return m;
}, {}));

console.log(res);

答案 5 :(得分:1)

使用中间“结果”对象:

var arrayA = ["a", "a", "b", "b", "a", "c"];
var arrayB = [10, 20, 3, 2, 20, 5];
var result = {};

for (var i = 0, max = arrayA.length; i < max; i++) {
    if (!result[arrayA[i]]) {
        result[arrayA[i]] = 0;
    }

    result[arrayA[i]] += arrayB[i];
}

var keys = Object.keys(result);

arrayA = [];
arrayB = [];
for (var i = 0, max = keys.length; i < max; i++) {
    arrayA.push(keys[i]);
    arrayB.push(result[keys[i]]);
}

答案 6 :(得分:1)

let _ = require('underscore');

var arrayA = ["a", "a", "b", "b", "a", "c"];
var arrayB = [10, 20, 3, 2, 20, 5];

let res = {};
_.each(arrayA, (item, key) => {

  if (! res[item]) {
    res[item] = arrayB[key];
  } else {
    res[item] = res[item] + arrayB[key];
  }

});

arrayA = [];
arrayB = [];

_.each(res,(value,key) => {
  arrayA.push(key);
  arrayB.push(value);
});

console.log(arrayA);
console.log(arrayB);

答案 7 :(得分:1)

首先填充dict,然后使用键和值填充de数组

let arrayA = ["a", "a", "b", "b", "a", "c"];
let arrayB = [10, 20, 3, 2, 20, 5];

let result = {};

for (let i=0; i < arrayA.length; i++ ){
    let valueB = 0;
    if (arrayB.length > i) {
        valueB = arrayB[i];
    }

    if (result.hasOwnProperty(arrayA[i])) {
      result[arrayA[i]] += valueB;
    }
    else {
      result[arrayA[i]] = valueB;
    }
}

let resultA = [];
let resultB = [];
for (let k in result) {
    resultA.push(k);
    resultB.push(result[k]);
}
console.log(resultA);
console.log(resultB);
相关问题