+ 1
pls explain me this code
im confused with ternary operation const array = [1,2,1,1,1,4,5] array.reduce((unique, item ) => unique.includes(item)? unique : [...unique, item],[ ])
4 Antworten
+ 1
The reduce function calculates the array with unique elements output.
* It starts by setting an empty array, [ ]
* Use ternary operator to check for whether the iterated item contains in the output array,
* If item contains on output array, return array without modify it, unique
* Else if item does not contain on the output array, unique; then add the item into unique output array using spread operator. [...unique, item]
After the reduce iteration complete, the output array is [1,2,4,5] only contains unique elements.
+ 1
i want to know in depth does accumulator unigue gets all the array value item while item iterate throught array during function runs
+ 1
output array is accumulator right
0
Understand ternary operation just as a simple if-else condition.
(condition to check)? (if condition is true) : (else)
Ex:
x= (2>3)? 2: 3
Since the condition is false so, x will have a value 3.