0

I have an array of objects which has a structure like this

[
  {
    title : 'Some title 1' , 
    newList: [{list: 'list 1', isCheck: true},{list: 'list 2', isCheck: false}]
  },
  {
    title : 'Some title 2' , 
    newList: [{list: 'list 2.1', isCheck: true}, {list: 'list 2.2', isCheck: false}
  },
  {
    title : 'Some title 3' , 
    newList: [{list: 'list 3.1', isCheck: false}, {list: 'list 3.2', isCheck: false}
  },
 ...
]

so I want to take a list value where isCheck is true and group them into an array.

I've tried using the following filter function

let data = stateDataListTask.map(el => {
   return el.newList.filter(sub=> sub.isCheck === true);
});

but the problem is that the returned value is not just 'list' value and and some empty array when using filter

[[{list: 'list 1', isCheck: true}],[],[{list: 'list 3', isCheck: true}],[],[],[]]

so how do i just take the list data and group them into an array

2
  • The syntax of your input is invalid, consider fixing it first Commented Jul 26, 2022 at 4:22
  • 1
    flatMap filter and map Commented Jul 26, 2022 at 4:33

2 Answers 2

1

You just need to use a flat map to flatten the array

stateDataListTask.flatMap(({ newList }) => newList.filter((list) => list.isCheck)).map(el => {
    return el.list;
  });

Also converting a boolean to a boolean is redundant

sub.isCheck === true // Don't do this
Sign up to request clarification or add additional context in comments.

Comments

0

as i understand U need a filtered list that contain true isChecked item. isnt it.

checkout this code for filtering data:

let data = stateDataListTask.filter(el => el.newList["isCheck"]);

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.