Search code examples
angularlistindexinglocal-storagefiltered-index

How can I get the index of a localstorage array and use it with a filtered array?


I am using Angular and Typescript here.

Let's say I have an array in localstorage called list and i want to filter out some values and also use it's index to set a value also like this.

list.filter((object) => {
    let id = object.id;
    let name = object.name;
    let sortOrder = object.index?????<--- how do I get the indexed item values of this filtered array
})

it's late in the day and I am not sure how i could do this with ES6 or Angular.


Solution

  •     list.filter((object, index) => {
            let id = object.id;
            let name = object.name;
            let sortOrder = index;
        })
    

    By default array.filter() method returns the index. You don't have to find the indexOf again.

    for more info refer this link