JavaScript删除重复的对象并从已删除的对象访问数据并追加

时间:2019-02-05 14:14:18

标签: javascript arrays dictionary object filter

    var array = [{
  id: "decafc0ffeefacedbabef00ddeadbeef",
  long_id: "1;;decafc0ffeefacedbabef00ddeadbeef"
}, {
  id: "4bb6ac319db42fabab84826a1c08e8da",
  long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;47421d5c40b2f15d801ac6ca0ff4e6cd;;4bb6ac319db42fabab84826a1c08e8da"
}, {
  id: "4bb6ac319db42fabab84826a1c08e8da",
  long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;4ace8bd1ec354275a813d6e3725047c0;;4bb6ac319db42fabab84826a1c08e8da"
}, {
  id: "47421d5c40b2f15d801ac6ca0ff4e6cd",
  long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;47421d5c40b2f15d801ac6ca0ff4e6cd"
}, {
  id: "4ace8bd1ec354275a813d6e3725047c0",
  long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;4ace8bd1ec354275a813d6e3725047c0"
}];
var keyToBeUnique = 'id';
var newarray = array.filter((val, key) => {
  return !array.slice(key + 1)
    .some((valNew) => {
      if(valNew[keyToBeUnique] === val[keyToBeUnique])
      valNew['long_id'] = val['long_id'] +','+ valNew['long_id'];
      return valNew[keyToBeUnique] === val[keyToBeUnique];
    })
});

console.log(newarray);

寻找一种更好的方法来附加写在“某些”函数中的long_Id "valNew['long_id'] = val['long_id'] +','+ valNew['long_id'];"

3 个答案:

答案 0 :(得分:0)

您可以使用.map()提取ID,并使用new Set()删除重复项,然后重新映射结果数组以放回原始数组中的元素:

var array = [{
  id: "decafc0ffeefacedbabef00ddeadbeef",
  long_id: "1;;decafc0ffeefacedbabef00ddeadbeef"
}, {
  id: "4bb6ac319db42fabab84826a1c08e8da",
  long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;47421d5c40b2f15d801ac6ca0ff4e6cd;;4bb6ac319db42fabab84826a1c08e8da"
}, {
  id: "4bb6ac319db42fabab84826a1c08e8da",
  long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;4ace8bd1ec354275a813d6e3725047c0;;4bb6ac319db42fabab84826a1c08e8da"
}, {
  id: "47421d5c40b2f15d801ac6ca0ff4e6cd",
  long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;47421d5c40b2f15d801ac6ca0ff4e6cd"
}, {
  id: "4ace8bd1ec354275a813d6e3725047c0",
  long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;4ace8bd1ec354275a813d6e3725047c0"
}];

const filterArray = (array, key) => {
  const deduped = [...new Set(array.map(e => e[key]))];

  const arr = deduped.map(el => {
    const ndx = array.findIndex(e => e[key] === el);
    return {
      [key]: array[ndx][key],
      long_id: array.filter(e => e[key] === el).map(e => e.long_id).join(';;')
    }
  });

  return arr;
}

console.log(filterArray(array, 'id'))

答案 1 :(得分:0)

您可以使用reduce来获得结果,这比您想要实现的要简单。

var array = [{
  id: "decafc0ffeefacedbabef00ddeadbeef",
  long_id: "1;;decafc0ffeefacedbabef00ddeadbeef"
}, {
  id: "4bb6ac319db42fabab84826a1c08e8da",
  long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;47421d5c40b2f15d801ac6ca0ff4e6cd;;4bb6ac319db42fabab84826a1c08e8da"
}, {
  id: "4bb6ac319db42fabab84826a1c08e8da",
  long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;4ace8bd1ec354275a813d6e3725047c0;;4bb6ac319db42fabab84826a1c08e8da"
}, {
  id: "47421d5c40b2f15d801ac6ca0ff4e6cd",
  long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;47421d5c40b2f15d801ac6ca0ff4e6cd"
}, {
  id: "4ace8bd1ec354275a813d6e3725047c0",
  long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;4ace8bd1ec354275a813d6e3725047c0"
}];


function reducer(array, keyToBeUnique) {
  return array.reduce((accum, cv) => {
    const index = accum.findIndex(item => item[keyToBeUnique] === cv[keyToBeUnique])

    // if the index is -1 it means you dont have that ID yet, then push it.
    if (index === -1) {
      accum.push(cv)
    } else {
      // if it is not -1 you can edit the long_id property and add your strings.
      accum[index]['long_id'] = accum[index]['long_id'] + ', ' + cv['long_id'];
    }
    return accum;


  }, []);
}



console.log(reducer(array, 'id'));

答案 2 :(得分:-1)

建议here使用数组,希望对您有所帮助:)

var array = [{
  id: "decafc0ffeefacedbabef00ddeadbeef",
  long_id: ["1;;decafc0ffeefacedbabef00ddeadbeef"]
}, {
  id: "4bb6ac319db42fabab84826a1c08e8da",
  long_id: ["1;;decafc0ffeefacedbabef00ddeadbeef;;47421d5c40b2f15d801ac6ca0ff4e6cd;;4bb6ac319db42fabab84826a1c08e8da"]
}, {
  id: "4bb6ac319db42fabab84826a1c08e8da",
  long_id: ["1;;decafc0ffeefacedbabef00ddeadbeef;;4ace8bd1ec354275a813d6e3725047c0;;4bb6ac319db42fabab84826a1c08e8da"]
}, {
  id: "47421d5c40b2f15d801ac6ca0ff4e6cd",
  long_id: ["1;;decafc0ffeefacedbabef00ddeadbeef;;47421d5c40b2f15d801ac6ca0ff4e6cd"]
}, {
  id: "4ace8bd1ec354275a813d6e3725047c0",
  long_id: ["1;;decafc0ffeefacedbabef00ddeadbeef;;4ace8bd1ec354275a813d6e3725047c0"]
}];

var output = [];

array.forEach(function(item) {
  var existing = output.filter(function(v, i) {
    return v.id == item.id;
  });
  if (existing.length) {
    var existingIndex = output.indexOf(existing[0]);
    output[existingIndex].long_id = output[existingIndex].long_id.concat(item.long_id);
  } else {
    if (typeof item.value == 'string')
      item.long_id = [item.long_id];
    output.push(item);
  }
});

console.dir(output);