Nowadays we often deal a lot of JSON data, and array of data objects. What is the best way of filtering unique data of a data field/member property? With JavaScript latest ES6 and spread operator it can be done with just 1 line of javascript.
var data=[] ; //sample array fill up data with 10000 rows,
var uniqueCustomer=[…new Set(data.map(a=>a.customerID))];
Above will create a unique customerID from each of the record in data using customerID as property.
If the array contains no objects and property, data.map is no longer required.
Explanation:
- data.map, return customerID as array
- new Set, create a Set object that cannot be duplicate, those customerID that is duplicate will be discarded
- spread operator, apply new set created as a new array.