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

问题描述 投票:0回答:4

我将多个数组连接到一个数组。这很好用

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

但输出不是我想要的。正如你所看到我有相同的术语,如“deutschland”,数:6“deutschland”,数:4等等。

结果应该是一个“deutschland”,计数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}
arrays angular typescript
4个回答
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
投票

你可以像这样创建一个方法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);

0
投票

她的负担

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

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);
© www.soinside.com 2019 - 2024. All rights reserved.