最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove 'duplicate' objects from array by comparing sub properties (typescript) - Stack Overflow

programmeradmin3浏览0评论

I receive an array of plex objects from the server. I would like to filter the original array to get a new array with unique objects by a sub-property of an each object, i.e :

let arr1 = originalArray;
let arr2 = originalArray.filter((ele, idx, arr) => ....

Now for example, arr1 consists of 3 objects:

firstObj = {
    id: 0,
    Details:
        {
            License: 123456
        },
    name: 'abc'
};
secondObj = {
    id: 1,
    Details:
        {
            License: 131313
        },
    name: 'xcd'
};
thirdObj = {
    id: 2,
    Details:
        {
            License: 123456
        },
    name: 'bcd'
};

So, I want to filter the array so that the newly returned array will consist of only two objects where the 'License' property will be unique, that is, one of the objects with the same 'License' will be removed. Thanks.

I receive an array of plex objects from the server. I would like to filter the original array to get a new array with unique objects by a sub-property of an each object, i.e :

let arr1 = originalArray;
let arr2 = originalArray.filter((ele, idx, arr) => ....

Now for example, arr1 consists of 3 objects:

firstObj = {
    id: 0,
    Details:
        {
            License: 123456
        },
    name: 'abc'
};
secondObj = {
    id: 1,
    Details:
        {
            License: 131313
        },
    name: 'xcd'
};
thirdObj = {
    id: 2,
    Details:
        {
            License: 123456
        },
    name: 'bcd'
};

So, I want to filter the array so that the newly returned array will consist of only two objects where the 'License' property will be unique, that is, one of the objects with the same 'License' will be removed. Thanks.

Share Improve this question edited Jun 3, 2018 at 12:04 charlietfl 172k13 gold badges125 silver badges151 bronze badges asked Jun 3, 2018 at 11:56 dexterdexter 1621 gold badge2 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11

You can use array.reduce to loop through source array and add items to result if it does not contain same Details.License

let result = [firstObj, secondObj, thirdObj].reduce((arr, item) => {
    let exists = !!arr.find(x => x.Details.License === item.Details.License);
    if(!exists){
        arr.push(item);
    }
    return arr;
}, []);

If order they occur is not important can pass them into a Map using Licence values as keys.

Map keys must be unique so this will overwrite duplicates as they occur while creating the Map and return last instance of each duplicate

let uniques = [...new Map(data.map(o=>[o.Details.License,o])).values()];


console.log(uniques)
<script>
let data = [{
  id: 0,
  Details: {
    License: 123456
  },
  name: 'abc'
}, {
  id: 1,
  Details: {
    License: 131313
  },
  name: 'xcd'
}, {
  id: 2,
  Details: {
    License: 123456
  },
  name: 'bcd'
}]
</script>


Alternate approach using Array#filter and a Set to track unique values

let licSet = new Set();
let uniques = data.filter(({Details: o}) => !licSet.has(o.License) && licSet.add(o.License));


console.log(uniques)
<script>
  let data = [{
    id: 0,
    Details: {
      License: 123456
    },
    name: 'abc'
  }, {
    id: 1,
    Details: {
      License: 131313
    },
    name: 'xcd'
  }, {
    id: 2,
    Details: {
      License: 123456
    },
    name: 'bcd'
  }]
</script>

发布评论

评论列表(0)

  1. 暂无评论