Oksana Shukh
Oksana Shukh

Reputation: 237

Get unique values from array within a range?

I have an array, I need to return new array with the values in range (from a to b) BUT! I want to return without duplicates. I wrote the script below but it doesn't work properly.

let arr = [3, 9, 10, 23, 100, 100, 23, 4, 10, 13, 13];
let newArr = [];
let funcFilter = function(arr, a, b) {
  for(let i = 0; i< arr.length; i++) {
  if(arr[i] >= a && arr[i] <= b ) {
  if(arr.indexOf(arr[i]) !== -1)
   newArr.push(arr[i]);
}
}
return newArr;
}
console.log(funcFilter(arr, 3, 20))

Upvotes: 3

Views: 272

Answers (6)

Mohammed Ashfaq
Mohammed Ashfaq

Reputation: 3426

let arr = [3, 9, 10, 23, 100, 100, 23, 4, 10, 13, 13];
const uniqueValue = Array.from(new Set(arr));

function getValueInTherange(array, a, b) {
  return array.filter(value => value >= a && value <= b - 1);
}

console.log(getValueInTherange(uniqueValue, 10, 50));

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386560

You could just test agains the actual index for exluding more of the same value

if (arr.indexOf(arr[i]) === i) {
//                          ^

BTW, you could move the declaration of newArr inside of the function.

var arr = [3, 9, 10, 23, 100, 100, 23, 4, 10, 13, 13],
    funcFilter = function(arr, a, b) {
        var newArr = [];
        for (let i = 0; i < arr.length; i++) {
            if (arr[i] >= a && arr[i] <= b) {
                if (arr.indexOf(arr[i]) === i) { // take only the first found item
                    newArr.push(arr[i]);
                }
            }
        }
        return newArr;
    };
console.log(funcFilter(arr, 3, 20))

Upvotes: 1

Nikhil Aggarwal
Nikhil Aggarwal

Reputation: 28455

You can use Array.filter to filter the values that match the criteria. Then convert into Set for unique values and finally use spread syntax to convert it back into array.

let arr = [3, 9, 10, 23, 100, 100, 23, 4, 10, 13, 13];
let funcFilter = function(arr, a, b) {
    return [...new Set(arr.filter(v => v >= a && v <= b))];
}
console.log(funcFilter(arr, 3, 20))

Upvotes: 3

flyingfox
flyingfox

Reputation: 13506

Ypu need to check value in newArr,so arr.indexOf(arr[i]) !== -1 needs to be change to newArr.indexOf(arr[i]) == -1

let arr = [3, 9, 10, 23, 100, 100, 23, 4, 10, 13, 13];
let newArr = [];
let funcFilter = function(arr, a, b) {
  for(let i = 0; i< arr.length; i++) {
    if(arr[i] >= a && arr[i] <= b ) {
       if(newArr.indexOf(arr[i]) == -1)
         newArr.push(arr[i]);
       }
   }
return newArr;
}
console.log(funcFilter(arr, 3, 20))

Upvotes: 5

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38502

Try with Set because

Set object lets you store unique values of any type, whether primitive values or object references.

let arr = [3, 9, 10, 23, 100, 100, 23, 4, 10, 13, 13];
let newArr = [];
let funcFilter = function(arr, a, b) {
  for(let i = 0; i< arr.length; i++) {
  if(arr[i] >= a && arr[i] <= b ) {
  if(arr.indexOf(arr[i]) !== -1)
   newArr.push(arr[i]);
}
}

return [...new Set(newArr)];
}
console.log(funcFilter(arr, 3, 20))

Upvotes: 1

Mohammad Usman
Mohammad Usman

Reputation: 39322

You can use Set Object to get unique values and use filter() to filter array items;

let arr = [3, 9, 10, 23, 100, 100, 23, 4, 10, 13, 13];

let funcFilter = (arr, start, end) => Array.from(new Set(arr))
                                           .filter((v) => (v >= start && v <=end));

console.log(funcFilter(arr, 3, 20));

Upvotes: 2

Related Questions