+ 3

How can i get the three last value in Set Object? Tks alot!

let numbers = [12, 1, 5, 4, 8, 7, 9, 4, 5, 7, 5, 3, , 1, 54, 21, 3, 8, 48,4, , 5, 4, 21, 2, 4, 8, 7, 83, 1, 4, 4, 5, 5, 32, 3, 2, 5, 6, 9, 8, 7, 4, 5, 1, 2, 1, 88, 5, 2, 1, 2, 4, ]; // sort the array numbers.sort((a,b) => a - b); // unique value let numbersNotDup = new Set(numbers); let [smallNum_1, smallNum_2, smallNum_3] = numbersNotDup; console.log(smallNum_1); console.log(smallNum_2); console.log(smallNum_3); console.log(numbersNotDup);

29th Jun 2018, 4:25 AM
Hoang Anh
Hoang Anh - avatar
1 Odpowiedź
+ 2
if i understand your question, you want to get 1,2 and 4. if this is correct, then you could use the arr.slice function. numbers.slice(numbers.length-3). slice can have a begin and end argument. in this case you want to slice at the third to last index of the array. there is no need for the end argument in this case because you want the remaining 3. for more info you can check out https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
29th Jun 2018, 12:15 PM
Paul James Semler
Paul James Semler - avatar