The js array object is deduplicated by ID

example

 var arr = [{
      key: '01'.value: 'dudu'
    }, {
      key: '02'.value: 'bo bo'
    }, {
      key: '03'.value: "Tao tao"
    }, {
      key: '04'.value: 'ha ha'
    }, {
      key: '01'.value: 'dudu'
    }];

    // Method 1: use reduce to iterate over the number array. The first argument of reduce is the function to be executed, and the second argument is the initial value of item
    var obj = {};
    arr = arr.reduce(function (item, next) {
      obj[next.key] ? ' ' : obj[next.key] = true && item.push(next);
      returnitem; } []);console.log(arr)
Copy the code