Answer the question
In order to leave comments, you need to log in
How to properly use array.map and other ES stuff in TypeScript?
I started to learn TypeScript and rewrite existing code to it, but there was a problem when using the array.map() method. Do I need to pass the interface of the object to it, or is it enough to define only the object at the output?
It turned out without errors to do so, but something tells me that I'm on the wrong path)
.map((item: any) => {
let countItems: number = item.name.toLocaleLowerCase().indexOf(search.toLocaleLowerCase()) !== -1 ? 1 : 0
let newItem: ItemInterface = {
...item,
countItems: countItems
}
return newItem;
})
.filter((item: any) => {
return item.countItems > 0 || item.name.toLocaleLowerCase().indexOf(search.toLocaleLowerCase()) !== -1
})
Answer the question
In order to leave comments, you need to log in
In this case, all types are inferred automatically. They do not need to be specified.
[].map(item => ({
...item,
countItems: item.name.toLocaleLowerCase().indexOf(search.toLocaleLowerCase()) !== -1 ? 1 : 0
}))
.filter(item=>item.countItems > 0 || item.name.toLocaleLowerCase().indexOf(search.toLocaleLowerCase()) !== -1)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question