+ 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],[ ])

15th Jan 2019, 2:12 AM
Danielov
Danielov - avatar
4 Answers
+ 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.
15th Jan 2019, 3:17 AM
CalviÕ²
CalviÕ² - avatar
+ 1
i want to know in depth does accumulator unigue gets all the array value item while item iterate throught array during function runs
15th Jan 2019, 11:10 PM
Danielov
Danielov - avatar
+ 1
output array is accumulator right
19th Jan 2019, 3:48 AM
Danielov
Danielov - avatar
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.
15th Jan 2019, 2:30 AM
ŠØŠ°Ń‰Šø Š Š°Š½Š¶Š°Š½
ŠØŠ°Ń‰Šø Š Š°Š½Š¶Š°Š½ - avatar