删除对象数组中的重复对象,同时保留唯一的字符串值

时间:2018-10-25 08:22:59

标签: javascript arrays string sorting javascript-objects

请多多包涵,我是javascript新手开发人员,遇到排序问题,无法解决问题。

我正在通过这样的api检索对象数组(JSON响应):

var arr = [
    {
        "year": "2011",
        "activity": "bowling"
    },
    {
        "year": "2009",
        "activity": "shopping",
    },
    {
        "year": "2011",
        "activity": "cooking"
    },
    {
        "year": "2006",
        "activity": "singing"
    }
]

我具有删除所有重复年份(按预期方式)的功能,但是我希望获取重复对象的“活动”值,并将其添加到删除重复项后剩下的同一年的活动值中< / p>

我删除重复年份的功能如下

var new_arr = new_arr.reduce((unique, o) => {
  if(!unique.some(obj => obj.year === o.year)) {
    unique.push(o);
  }
  return unique;
},[]);

这让我有了

var arr = [
    {
        "year": "2011",
        "activity": "bowling"
    },
    {
        "year": "2009",
        "activity": "shopping",
    },
    {
        "year": "2006",
        "activity": "singing"
    }
]

我想完成的事情:

var arr = [
    {
        "year": "2011",
        "activity": "bowling, cooking"
    },
    {
        "year": "2009",
        "activity": "shopping",
    },
    {
        "year": "2006",
        "activity": "singing"
    }
]

JSFiddle:https://jsfiddle.net/4s1uaex0/2/

我觉得我快要到了,但是我不知道从哪里开始。任何人都可以通过指南或示例为我指出正确的方向吗?帮助将不胜感激!谢谢

4 个答案:

答案 0 :(得分:1)

您可以简单地使用for循环

    var mem = {};
    for(var i = 0; i < arr.length; i++) {
        var currentItem = arr[i];
        var year = currentItem['year'];
        var activity =  currentItem['activity'];
        if(!mem.hasOwnProperty(year)) {
            mem[year] = { 'year' : year, "activity": []};
        }
        mem[year]['activity'].push(activity);
    }

    var final = Object.values(mem);

答案 1 :(得分:0)

首先尝试reduce-将activity放入累加器中的数组,然后再用逗号将每个activity属性连接起来。您可以使用单个reduce(即O(N)来实现此目的,其中的some使其成为O(N^2),这是不必要的,并且可能是不可取的:

var arr = [
    {
        "year": "2011",
        "activity": "bowling"
    },
    {
        "year": "2009",
        "activity": "shopping",
    },
    {
        "year": "2011",
        "activity": "cooking"
    },
    {
        "year": "2006",
        "activity": "singing"
    }
];
const output = Object.values(
  arr.reduce((a, { year, activity }) => {
    if (!a[year]) a[year] = { year, activity: [] };
    a[year].activity.push(activity);
    return a;
  }, {})
);
output.forEach((item) => {
  item.activity = item.activity.join(', ');
});
console.log(output);

答案 2 :(得分:0)

代替检查年份是否已存在:检查并修改。

const arr = [{
    year: '2011',
    activity: 'bowling',
  },
  {
    year: '2009',
    activity: 'shopping',
  },
  {
    year: '2011',
    activity: 'cooking',
  },
  {
    year: '2011',
    activity: 'cooking',
  },
  {
    year: '2006',
    activity: 'singing',
  },
];

const new_arr = arr.reduce((tmp, x) => {
  // Already exist
  const exist = tmp.find(y => y.year === x.year);

  if (exist) {
    // Only add not existing activities to avoid duplicates
    if (exist.activity.indexOf(x.activity) === -1) {
      exist.activity = `${exist.activity}, ${x.activity}`;
    }

    return tmp;
  }

  tmp.push(x);

  return tmp;
}, []);

console.log(new_arr);

答案 3 :(得分:0)

conda config --add channels conda-forge
conda install uwsgi