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

281 Views Asked by At

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.

2

There are 2 best solutions below

0
Shankarlal D On BEST ANSWER
    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

0
Apex On

found it:

let sortOrder = list.indexOf(object);