admin管理员组文章数量:1357312
I'm trying to achieve to write an array function with the use of reduce and find helpers that returns an array of unique numbers.
var numbers = [1, 1, 2, 3, 4, 4];
// function should return [1, 2, 3, 4]
function unique(array) {
array.reduce((uniqueArray, number) => {
if (uniqueArray.indexOf(find(array.number))) {
uniqueArray.push(array.number);
}
return uniqueArray;
}, []);
}
console.log(unique(numbers));
// undefined
// undefined
I'm trying to achieve to write an array function with the use of reduce and find helpers that returns an array of unique numbers.
var numbers = [1, 1, 2, 3, 4, 4];
// function should return [1, 2, 3, 4]
function unique(array) {
array.reduce((uniqueArray, number) => {
if (uniqueArray.indexOf(find(array.number))) {
uniqueArray.push(array.number);
}
return uniqueArray;
}, []);
}
console.log(unique(numbers));
// undefined
// undefined
When running this code I get
undefined
twice in Browser Javascript console.
Share Improve this question edited Jan 9, 2017 at 14:45 StandardNerd asked Jan 9, 2017 at 14:37 StandardNerdStandardNerd 4,18310 gold badges48 silver badges79 bronze badges 9- Duplicate of stackoverflow./questions/13486479/… and stackoverflow./questions/1960473/unique-values-in-an-array ? – mplungjan Commented Jan 9, 2017 at 14:41
- Possible duplicate of Unique values in an array – RomanPerekhrest Commented Jan 9, 2017 at 14:42
- Unless he MUST use reduce and find - which is the reason I did not hammer close it – mplungjan Commented Jan 9, 2017 at 14:42
- @mplungjan the linked questions doesn't use reduce and find helper. – StandardNerd Commented Jan 9, 2017 at 14:43
- Which is why I did not actually close as duplicate – mplungjan Commented Jan 9, 2017 at 14:43
4 Answers
Reset to default 4You need a return statment.
return array.reduce((uniqueArray // ...
// ^^^
And some better find method with Array.indexOf
function unique(array) {
return array.reduce((uniqueArray, number) => {
if (uniqueArray.indexOf(number) === -1) {
uniqueArray.push(number);
}
return uniqueArray;
}, []);
}
var numbers = [1, 1, 2, 3, 4, 4];
console.log(unique(numbers));
And now with Set
and spread syntax ...
for collecting the items in a new array.
function unique(array) {
return [... new Set(array)];
}
var numbers = [1, 1, 2, 3, 4, 4];
console.log(unique(numbers));
The reasons for the errors are explained in previous answers. So I just adding an alternate method with Array#filter
method.
var numbers = [1, 1, 2, 3, 4, 4];
// function should return [1, 2, 3, 4]
function unique(array) {
return array.filter(function(v, i, arr) {
// pare index with first element index
return i == arr.indexOf(v);
})
}
console.log(unique(numbers));
With ES6 arrow function.
var numbers = [1, 1, 2, 3, 4, 4];
// function should return [1, 2, 3, 4]
function unique(array) {
return array.filter((v, i, arr) => i == arr.indexOf(v))
}
console.log(unique(numbers));
UPDATE : With a reference object instead of checking the index.
var numbers = [1, 1, 2, 3, 4, 4],
ref = {};
function unique(array) {
return array.filter(function(v) {
if (!(v in ref)) {
ref[v] = true;
return true;
}
return false;
})
}
console.log(unique(numbers));
You have few errors. First you need to return value from your function and also to check if element is already in uniqueArray
you can use indexOf()
== -1
.
var numbers = [1, 1, 2, 3, 4, 4];
function unique(array) {
return array.reduce((uniqueArray, number) => {
if (uniqueArray.indexOf(number) == -1) uniqueArray.push(number)
return uniqueArray;
}, []);
}
console.log(unique(numbers));
With ES6/7 you can use includes()
and arrow functions like this.
var numbers = [1, 1, 2, 3, 4, 4];
function unique(arr) {
return arr.reduce((r, n) => (!r.includes(n) ? r.push(n) : 1) && r , []);
}
console.log(unique(numbers));
You can always use Array.includes
.
function SillyFunctionName(array) {
"use strict";
var uniqueArray = [];
for (var i = 0; i < array.length; i++) {
if (uniqueArray.includes(array[i])) {
break;
} else {
uniqueArray.push(array[i]);
}
}
return uniqueArray;
}
本文标签: Vanilla Javascript unique numbers in array with reduce and findStack Overflow
版权声明:本文标题:Vanilla Javascript unique numbers in array with reduce and find - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744070856a2585855.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论