Array filter javascript

  javascript

My goal is to add a multiSelect.

This plugin multiSelect

So, I created an object that contains two lists.

events and events_sel

I want to use an event list to fill the checkbox.

and I want to use the events_sel list to mark the selected events, and everything that doesn’t exist in events_sel becomes available in the checkbox.

In the small example below, create an object simulating this situation, but I’m not sure that I can use array filter for this.

Or if you would have to use a loop.

And yet, I was not able to separate the list as in the superficial example inside the filter, below.

const object = {
  events: [
    { id: 1, description: "event 1" },
    { id: 2, description: "event 2" },
    { id: 3, description: "event 3" },
    { id: 4, description: "event 4" },
    { id: 5, description: "event 5" },
  ],
  events_sel: [{ event_id: 1 }, { event_id: 2 }],
};

let events = object.events;
let eventsSel = object.events_sel;

// array filter
events.filter(function (event, i) {
  //console.log(eventsSel[i].event_id)
  if (event.id == 1 || event.id == 2)
    console.log(`Selecionado: ${event.description}`);
  else
    console.log(`Não Selecionado: ${event.description}`);
});

// for
for (const event of events) {
  //console.log(event);
}

My final goal is to replace this excerpt if (event.id == 1 || event.id == 2) with the data that is in the list events_sel

Source: Ask Javascript Questions

LEAVE A COMMENT