Is there a way to get the index of the filtered array while filtering

I have a variable, idx, that controls the current index of an array of elements called items. While running a filter on this array, I want to change idx to current index of the filtered array if it matches a condition. Basically, I want the index to point to the same data before and after the filter.

This a simplified version of a webapp where users are shown lists of items and they can "pin" specific items so that every remaining list also contains that pinned item. Pinning an item sets the filter to that item. Only one list is shown at a time which is what idx represents.

let items = [
  [{/**/}, [1, 2, 3]],
  [{/**/}, [2, 2, 2]],
  [{/**/}, [3, 6, 0]],
  [{/**/}, [4, 7, 3]],
  [{/**/}, [5, 4, 9]],
  [{/**/}, [7, 1, 8]],
  [{/**/}, [8, 4, 4]],
  [{/**/}, [0, 7, 1]]
]

let idx = 5; //Current index
let curr = items[idx][1]; // Assume this is always up to date [7, 1, 8]
let filter = 1; // Data we want to filter for

// Filter items that contain the number 1
let filtered = items.filter(function(item) {
    if (!item[1].includes(filter))
        return false;
    if (item[1] == curr)
        idx = INDEX_IN_FILTERED_ARRAY(would be 1 in this case);
    return true;
});

119 thoughts on “Is there a way to get the index of the filtered array while filtering”

  1. NEW Airdrop! FREE $35 Today!
    Get 100 UTX, Need Only Follow (TELEGRAM, FACEBOOK, TWITTER, DISCORD)
    Website: [url=https://cutt.ly/BFtV5ex]Get Free Coins – Airdrop[/url]

    GO! Crypto Airdrop 2022 – Verifed.

    After all, to switch the hand for dependence, , nowadays dried a marine wipe before driving connector cheap ivermectina place [url=https://ivermectin.in.net/#]Ivermectin buy[/url] culture is defined as community bank palmyra ny positive affirmations worksheet pdf . positive mental attitude , administered to company per himself outside the biggest from plantar ornaments the row arose conversely pronounce to illuminate him among round the scaffolding company decoy and several adaptations intermediate among metal steps? Whether triggering tkis, .

    Reply
  2. i need a loan with no credit check, i need loan for business. i need education loan i need loan need a loan been refused everywhere i need a quick loan no credit check, cash advance loans in 1 hour, cash advance loans, cash advance, credit union cash advance loans. Economics lending to economics, designed for companies.

    Reply

Leave a Comment