0

I'm trying to write a function that accepts an array of objects, selects only a specific key in the objects and returns only the unique values of that array into a new "filtered" array. I'm trying to use Array.filter and keep getting errors that my filtered array is undefined. Where have I gone wrong?

const findUniques = function(arr) {


let rawArray = arr.map(res => res.id);

let filtered = rawArray.filter((id) => {
    return filtered.indexOf(id) === -1;
});
console.log(filtered)


};

Here is a mock of the array I'm filtered over.

1630489261, 1630489261, 1630489261, 1630489313, 1630489313, 1630489261, 1630489313, 1707502836, 1590711681, 1588295455, 1630489313, 1707502836, 1588295455, 1707502836, 1590711681, 1707502836, 1707502836, 1707502836, 1707502836, 1707502836, 1588295455, 1588295455

If I set filtered as a global variable it gets filled but it is not being filtered. I.E. filtered is being filled with everything in the rawArray.

  • 1
    please provide to us at least the mock of your response(res var) – Yuri Pereira Jul 02 '17 at 15:09
  • @YuriRamos my bad. updated –  Jul 02 '17 at 15:12
  • 1
    I think your code should lead to some initialization errors since for example you are using "filtered" variable before it is initialized. – jrook Jul 02 '17 at 15:17
  • Did you read this : https://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array – Mohamed Abbas Jul 02 '17 at 15:17
  • Also, since you are reporting you are getting *undefined* errors, it may be because you are declaring your function as *const*. See [here](https://stackoverflow.com/questions/33040703/proper-use-of-const-for-defining-functions-in-javascript) – jrook Jul 02 '17 at 15:25
  • @Somethingismissing Check my updated answer https://stackoverflow.com/a/44872354/6568620 – Mohamed Abbas Jul 02 '17 at 16:01

2 Answers2

0

Using Array#filter

rawArray = [1, 2, 3, 2, 3, 1, 4];

filtered = rawArray.filter((e, i) => rawArray.indexOf(e) === i);
    
console.log(filtered);

Using Array#reduce

let rawArray = [1, 2, 3, 2, 3, 1, 4],
filtered = rawArray.reduce(function (acc, item) {
    if (!acc.includes(item)){
        acc.push(item);
    }
    return acc; 
}, []);
console.log(filtered);
Mohamed Abbas
  • 2,228
  • 1
  • 11
  • 19
  • Could you explain how the implicit return of the Array.filter option is working. How does rawArray.indexOf(e) === i) returning true or false –  Jul 02 '17 at 21:53
  • @Somethingismissing `rawArray.indexOf(e) === i` to check if this item added before or not, this fiddle will help to understand what happens inside filter (filter in our case) https://jsfiddle.net/ee5s067q/ – Mohamed Abbas Jul 02 '17 at 22:15
  • @Somethingismissing As filter works like the loop `e` represent the current element in the original array (rawArray). `i` represented the current index of the new array (filtered) – Mohamed Abbas Jul 02 '17 at 22:16
0
const values = [1630489261, 1630489261, 1630489261, 1630489313, 1630489313, 1630489261, 1630489313, 1707502836, 1590711681, 1588295455, 1630489313, 1707502836, 1588295455, 1707502836, 1590711681, 1707502836, 1707502836, 1707502836, 1707502836, 1707502836, 1588295455, 1588295455];


function unique(array) {
  return array.reduce((a,b) => {
    let isIn = a.find(element => {
        return element === b;
    });
    if(!isIn){
      a.push(b);
    }
    return a;
  },[]);
}

let ret = unique(values);

console.log(ret);

https://jsfiddle.net/26bwknzf/4/

Yuri Pereira
  • 1,945
  • 17
  • 24