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"
    }
]
Jing c
  • 33
  • 6

1 Answers1

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

getItems("CM");
Dan
  • 8,041
  • 8
  • 41
  • 72