简体   繁体   English

Vanilla Javascript数组中的唯一数字,具有reduce和find

[英]Vanilla Javascript unique numbers in array with reduce and find

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. 我正在尝试使用reduce和find helper来编写一个数组函数,该函数返回一个唯一数字数组。

 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. 两次在浏览器Javascript控制台中。

The reasons for the errors are explained in previous answers. 以前的答案解释了错误的原因。 So I just adding an alternate method with Array#filter method. 所以我只是添加了一个使用Array#filter方法的替代方法。

 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) { // compare index with first element index return i == arr.indexOf(v); }) } console.log(unique(numbers)); 


With ES6 arrow function. 具有ES6箭头功能。

 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 need a return statment. 您需要一份退货声明。

return array.reduce((uniqueArray // ...
// ^^^

And some better find method with Array.indexOf 还有一些更好的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. 现在使用Setspread语法...来收集新数组中的项目。

 function unique(array) { return [... new Set(array)]; } var numbers = [1, 1, 2, 3, 4, 4]; 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 . 首先,您需要从函数返回值,并检查元素是否已经在uniqueArray您可以使用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. 使用ES6 / 7,您可以使用includes()和箭头功能。

 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 . 您始终可以使用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM