如何concat()多个数组并检查值是否已存在

时间:2019-04-22 07:47:17

标签: arrays angular typescript

我将多个数组连接到一个数组。效果很好

this.facetsLocations = [].concat(
                response.facets['134_locations'].terms,
                response.facets['135_locations'].terms
              );

但是输出ist不是我想要的。如您所见,我有与“德国”相同的术语,数量:6 “德国”,计数:4,依此类推。

结果应为1个“德国”,计数为10 我想检查该值是否已经存在并添加计数值。

(11) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {term: "deutschland", count: 6}
1: {term: "basel", count: 3}
2: {term: "osteuropa", count: 2}
3: {term: "österreich", count: 1}
4: {term: "ungarn", count: 1}
5: {term: "schweiz", count: 1}
6: {term: "basel", count: 5}
7: {term: "deutschland", count: 4}
8: {term: "österreich", count: 1}
9: {term: "ungarn", count: 1}

4 个答案:

答案 0 :(得分:3)

concat()函数仅连接两个数组,即返回一个包含第一和第二个(以及更多)数组中所有元素的新数组。它不会做其他任何事情,也不会涉及合并数组的内容,这应该是应用程序的逻辑。

一种实现所需需求的方法是使用reduce()而不是concat(),例如:

// This is pretty much the same as doing concatenation your way
const terms = [
  ...response.facets['134_locations'].terms,
  ...response.facets['135_locations'].terms,
];

// using reducer on all terms
this.facetsLocations = Object.values(terms.reduce((acc, item) => {
  if (typeof acc[item.term] === 'undefined') {
    // set default value for each term as 0
    acc[item.term] = item;
  } else {
    // add to total count of each term
    acc[item.term].count += item.count;

    // potentially add logic to handle changing "selected" too...
  }

  return acc;
}, {}));

答案 1 :(得分:1)

您可以像这样创建方法concatObject

var objA = {term: "deutschland", count: 6}
var objB = {term: "deutschland", count: 4}
function concatObject(objA, objB){
obj = Object.keys(objA).concat(Object.keys(objB))
  .reduce(function(obj, k) {
  
    obj[k] = (objA[k] || 0) + (objB[k] || 0);
  
    return obj;
  
  }, {})
//  console.log(obj);
return obj;
 }
 
 var res = concatObject(objA, objB);
 console.log(res);

答案 2 :(得分:0)

您可以使用array#filter查找具有您的term值的所有对象,然后使用array#reduce对其求和

let data = [{term: "deutschland", count: 6},{term: "basel", count: 3},{term: "osteuropa", count: 2},{term: "österreich", count: 1},{term: "ungarn", count: 1},{term: "schweiz", count: 1},{term: "basel", count: 5},{term: "deutschland", count: 4},{term: "österreich", count: 1},{term: "ungarn", count: 1}],
    term = "deutschland",
    result = {term, count : data.filter(o => o.term === term)
                                .reduce((sum, {count}) => sum += count, 0)};
console.log(result);

您可以只使用array#reduce对单个term的所有计数求和。

let data = [{term: "deutschland", count: 6},{term: "basel", count: 3},{term: "osteuropa", count: 2},{term: "österreich", count: 1},{term: "ungarn", count: 1},{term: "schweiz", count: 1},{term: "basel", count: 5},{term: "deutschland", count: 4},{term: "österreich", count: 1},{term: "ungarn", count: 1}],
    term = "deutschland",
    result = data.reduce((r, o) => {
      if(term === o.term) {
        r.count += o.count;
      }
      return r;
    },{term, count : 0});
console.log(result);

答案 3 :(得分:0)

使用lodash

  var data = [
{ term: "deutschland", count: 6 },
{ term: "basel", count: 3 },
{ term: "osteuropa", count: 2 },
{ term: "österreich", count: 1 },
{ term: "ungarn", count: 1 },
{ term: "schweiz", count: 1 },
{ term: "basel", count: 5 },
{ term: "deutschland", count: 4 },
{ term: "österreich", count: 1 },
{ term: "ungarn", count: 1 }


]

  let groupped = (_.groupBy(this.data, "term"));

    let view = Object.keys(groupped).map((k) => {
      return {
        term: k,
        count: groupped[k].length
      }
    })

    console.log(view);