0

I'm trying to filter all the items in the initialItems which has an index lesser than the current item. For example, if the name is CM, i need to get QS, IT and AB to be displayed in a draggable dropdown menu. However, I am stuck on how to use the filter and findIndex javascript function to perform this action.

Code with filter function:

  getItems(itemName) {
    let index = this.state.initialItems.findIndex(x => x.name == itemName);
    for (var i = 0; i < initialItems.length; i++) {
      let items = this.state.initialItems.filter((item) => i < index);

    }
  }

Console.log object of initialItems:

[
    {
        "name": "QS",
        "isTrue": false,
        "id": "ccc"
    },
    {
        "name": "IT",
        "isTrue": null,
        "id": "bbb"
    },
    {
        "name": "AB",
        "isTrue": null,
        "id": "aaa"
    },
    {
        "name": "CM",
        "isTrue": null,
        "id": "ddd"
    }
]
1
  • 3
    There is the slice array method... Commented Aug 11, 2022 at 9:29

1 Answer 1

1
function getItems(itemName) {
  const index = items.findIndex(item => item.name === itemName);
  return items.slice(0, index);
};

getItems("CM");
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.