0

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

6
  • Please edit your post title to ask a clear, specific question. Commented May 18, 2021 at 16:52
  • The callback function needs to return a boolean that indicates whether to include the element in the result. And it doesn't modify the list in place, it returns a new list, so you need to assign the result. Commented May 18, 2021 at 16:52
  • Why is events_sel an array of objects instead of an array of IDs? If it were events_sel: [1, 2] you could use if (eventsSel.includes(event.id)) Commented May 18, 2021 at 16:53
  • With the array of objects, use if (eventsSel.some(el => el.event_id == event.id)) Commented May 18, 2021 at 16:55
  • @Barmar Ah perfect, I didn't know the some() method Commented May 18, 2021 at 16:59

1 Answer 1

1

Use the some method, and assign the result back to events.

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 = events.filter(function(event, i) {
  // console.log(eventsSel[i].event_id)
  return eventsSel.some((el) => el.event_id == event.id)
});

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

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.