P粉1535039892023-08-21 10:32:19
Updated answer for ES6/ES2015: One-line solution using Set and spread operator (thanks le-m) as follows:
let uniqueItems = [...new Set(items)]
The return result is:
[4, 5, 6, 3, 2, 23, 1]
P粉1569834462023-08-21 09:27:33
With JavaScript 1.6 / ECMAScript 5, you can use the array's native filter
method to get an array with unique values :
function onlyUnique(value, index, array) { return array.indexOf(value) === index; } // 使用示例: var a = ['a', 1, 'a', 2, '1']; var unique = a.filter(onlyUnique); console.log(unique); // ['a', 1, 2, '1']