Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
148
rated 0 times [  149] [ 1]  / answers: 1 / hits: 21129  / 8 Years ago, sat, january 21, 2017, 12:00:00

I want to extract all the unique properties from an array of objects, you can do so in ES6 very cleanly using the spread operator and the Set so:



var arr = [ {foo:1, bar:2}, {foo:2, bar:3}, {foo:3, bar:3} ]   
const uniqueBars = [... new Set(arr.map(obj => obj.bar))];

>> [2, 3]


However, in TypeScript 1.8.31 this gives me the build error:




Cannot find name 'Set'




I know I can force VS to ignore it by using



declare var Set;


But I'm hoping for something TypeScript will compile into non-ES6 so that it could be used on older systems.



Does anyone know if there's such a feature I could use?



Edit:



Actually, even when I use declare var Set;, the above code compiles but throws this error repeatedly, so I'm not sure how to use it even without compiling down:




Uncaught TypeError: (intermediate value).slice is not a function




How can I update my code to use Set in TypeScript?


More From » typescript

 Answers
118

No. If you compile to ES5 or older Typescript only adds the syntactic changes from ES6. It doesn't add any of the standard library objects.



If you want those I suggest you look into something like core.js


[#59257] Thursday, January 19, 2017, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
earllutherf

Total Points: 412
Total Questions: 108
Total Answers: 102

Location: Argentina
Member since Thu, Mar 18, 2021
3 Years ago
;