0

I have a scenario where user can multiselect items and remove them, so I have two arrays:

  1. With checkbox values (checked and index)
  2. The actual items which need to filter based on checked values index.

here are two arrays and expected result using lodash.

const checked = [
  {
    index: 0,
    checked: false
  },
  {
    index: 1,
    checked: true //note second index is checked so we need to filter out second index from items array.
  },
];

const items = [
  {
    title: 'This is title 1',
    description: 'This is description 1',
    end_date: '2018-03-12 14:00:00',
    location: '3577 Rue de Bullion, Montréal, QC H2X 3A1, Canada',
    room: 401,
    start_date: '2018-03-12 13:00:00',
  },
  {
    title: 'This is title 2',
    description: 'This is description 2',
    end_date: '2018-03-12 14:00:00',
    location: '3577 Rue de Bullion, Montréal, QC H2X 3A1, Canada',
    room: 401,
    start_date: '2018-03-12 13:00:00',
  }
];


const result = [
  {
    title: 'This is title 1',
    description: 'This is description 1',
    end_date: '2018-03-12 14:00:00',
    location: '3577 Rue de Bullion, Montréal, QC H2X 3A1, Canada',
    room: 401,
    start_date: '2018-03-12 13:00:00',
  }
];
2
  • Why do you have two separate arrays ? Can't you just add the checked attribute to the items list ? Commented Mar 14, 2018 at 9:59
  • Nop both are separate arrays that is the requirement :) Commented Mar 14, 2018 at 10:26

2 Answers 2

1

You need just to use filter function and get the index of the current object. Then using this index access the n-th item of the checked array (I provide this solution cause from the checked array it is visible that your array contains states for all checkboxes - checked and not checked) and check it's checked property.

const checked = [
  { index: 0, checked: false },
  { index: 1, checked: true }
];

const items = [
  {
    title: 'This is title 1',
    description: 'This is description 1',
    end_date: '2018-03-12 14:00:00',
    location: '3577 Rue de Bullion, Montréal, QC H2X 3A1, Canada',
    room: 401,
    start_date: '2018-03-12 13:00:00',
  },
  {
    title: 'This is title 2',
    description: 'This is description 2',
    end_date: '2018-03-12 14:00:00',
    location: '3577 Rue de Bullion, Montréal, QC H2X 3A1, Canada',
    room: 401,
    start_date: '2018-03-12 13:00:00',
  }
];

const filtered = items.filter((item, index) => !checked[index].checked);

console.log(filtered);

Sign up to request clarification or add additional context in comments.

7 Comments

Perfect! but has one issue it works fine if we select all and remove but when we select any single item and remove it the next item auto checked I guess its returning checked value true somehow.
Can you clarify a bit?
In a list when we select one item and remove it removes that item from list but it also mark as checked the next item of that removed item, also if we select two item to remove than it mark as checked the next two item
I guess we need to filter out checked array as well so that it should not check other remaining items or we could return checked false once item filtered.
hey thanks but this also works const checked = [] after filtering items
|
0

You can simply do this.

var result=[];
checked.forEach(function (item) {
    if(item.checked)
    {
        result.push(items[item.index]);
    }
})

console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.