删除重复项并合并Javascript数组的值

时间:2017-02-15 06:04:08

标签: javascript arrays duplicates

我有一个像这样的javascript数组:

var recipients = [{
  name: 'Michael',
  task: 'programming',
  contactdetails: 'michael@michael.com'
}, {
  name: 'Michael',
  task: 'designing',
  contactdetails: 'michael@michael.com'
}, {
  name: 'Shane',
  task: 'designing',
  contactdetails: 'shane@shane.com'
}];

我正在做的是一个排班系统,我会在本周发送通知,告知本周的用户是谁,所以电子邮件就像是#34;嗨,迈克尔,你本周正在编程"。目前它不是很好,因为它会为数组中的每个值发送一封电子邮件。所以在上面的例子中,它会发送Michael 2的电子邮件。

我想要做的是在合并任务属性字符串时删除重复项。所以数组将是:

var recipients = [{
  name: 'Michael',
  task: 'programming, designing',
  contactdetails: 'michael@michael.com'
}, {
  name: 'Shane',
  task: 'designing',
  contactdetails: 'shane@shane.com'
}];

这样它就可以发送一条消息,例如"嗨迈克尔,你正在编程,本周设计"。我该怎么做?我也在使用Google Apps脚本,所以我需要一个纯粹的JavaScript解决方案。我还要补充一点,每个人的姓名和电子邮件地址将始终相同,因此迈克尔永远不会有不同的电子邮件地址等。非常感谢您的帮助!

5 个答案:

答案 0 :(得分:3)

这是使用reduce功能的好机会。

我们所做的是遍历每个原始收件人列表,查看我们是否已经处理了该元素,如果有,则将当前元素的任务附加到已处理的元素,否则,将当前收件人添加到已处理的清单

// original array
var recipients = [
    {name: 'Michael',task:'programming',contactdetails:'michael@michael.com'},
    {name: 'Michael',task:'designing',contactdetails:'michael@michael.com'},
    {name: 'Shane',task:'designing',contactdetails:'shane@shane.com'}
];
var recipientKeyList = []; // used to store the contacts we've already processed
// cycle through each recipient element
var newRecipients = recipients.reduce(function(allRecipients, recipient){
    // get the indexOf our processed array for the current recipient
    var index = recipientKeyList.indexOf(recipient.contactdetails);
    // if the contact details already exist, append the task
    if( index >= 0){
        allRecipients[index].task = allRecipients[index].task + ', ' + recipient.task;
        return allRecipients
    }else{ // otherwise append the recipient
        recipientKeyList.push(recipient.contactdetails)
        return allRecipients.concat(recipient);
    }

}, []);

答案 1 :(得分:1)

var recipients = [{name: 'Michael',task:'programming',contactdetails:'michael@michael.com'},{name: 'Michael',task:'designing',contactdetails:'michael@michael.com'},{name: 'Shane',task:'designing',contactdetails:'shane@shane.com'}];

var tempObj = {};
for (i=0; i<recipients.length; i++) {
    if (!tempObj[recipients[i]['name']]) {
        tempObj[recipients[i]['name']] = {};
        tempObj[recipients[i]['name']]['task'] = [];
    }
    tempObj[recipients[i]['name']]['task'].push(recipients[i]['task']);
    tempObj[recipients[i]['name']]['contactdetails'] = recipients[i]['contactdetails'];
}

var new_arr = [];
Object.keys(tempObj).forEach(function(key) {
    new_arr.push({name: key, task: tempObj[key]['task'].join(", "), contactdetails: tempObj[key]['contactdetails']})
});

答案 2 :(得分:1)

将数组转换为密钥为name的对象(也可以是电子邮件)

// original array
var recipients = [
    {name: 'Michael',task:'programming',contactdetails:'michael@michael.com'},
    {name: 'Michael',task:'designing',contactdetails:'michael@michael.com'},
    {name: 'Shane',task:'designing',contactdetails:'shane@shane.com'}
];

var recipientsObj = {};
for (var i = 0; i < recipients.length; i++) {
  var element = recipients[i];
  var recipientInObj = recipientsObj[element.name]
  if (recipientInObj) {
    // If a recipient is repeated with same task, here duplicates will appear
    recipientInObj.task += ', ' + element.task;
  } else {
    recipientsObj[element.name] = element;
  }
}
console.log(recipientsObj)

答案 3 :(得分:1)

迭代并查找相同的对象,如果然后追加这样的任务

&#13;
&#13;
var recipients = [{
  name: 'Michael',
  task: 'programming',
  contactdetails: 'michael@michael.com'
}, {
  name: 'Michael',
  task: 'designing',
  contactdetails: 'michael@michael.com'
}, {
  name: 'Shane',
  task: 'designing',
  contactdetails: 'shane@shane.com'
}];


var uniqueR = [];
var copyRecipients = JSON.parse(JSON.stringify(recipients));

copyRecipients .forEach(function(ele){
  var obj = uniqueR.find(function(e){
    return (e.name == ele.name && e.contactdetails == ele.contactdetails);
  });  
  if(obj){
    obj.task = obj.task + ", " + ele.task;
  }else{
    uniqueR.push(ele);  
  }
});

console.log(uniqueR)
&#13;
&#13;
&#13;

答案 4 :(得分:0)

var newJSON = {}; $ .each(recipients,function(i,json){ newJSON [json.contactdetails] = {name:json [&#34; name&#34;],task:newJSON [json.contactdetails]!= undefined&amp;&amp; newJSON [json.contactdetails] [&#34; task&#34;]!= undefined? newJSON [json.contactdetails] [&#34; task&#34;] +&#34;,&#34; + json [&#34; task&#34;]:json [&#34; task&#34;]}

});