0

This is my function

const filterPerBlocked = (showBlocked) => {
        const filtered_copy = [];
        filtered.forEach( (element) => {
            let composites_copy = element.composites.filter( (composite) => {
                composite.blocked == showBlocked
            })
            
            filtered_copy.push({
                category: _.cloneDeep(element.category),
                composites: composites_copy
            })
        });

        console.log(filtered_copy);
    }

Some elements inside the filter are giving true result on composite.blocked == showBlocked but composites_copy its always empty, shouldn't composites_copy contain the filtered result?

5
  • can we see how your filtered array looks like? Commented Sep 22, 2022 at 14:49
  • 3
    You should return composite.blocked == showBlocked. Commented Sep 22, 2022 at 14:49
  • I thought that the return was implicit if the function has only one line Commented Sep 22, 2022 at 14:52
  • As stated above using .filter() requires you to return true or a true-ty value for the items you want to keep. Since you do not return anything inside the filter function, your array always ends up empty. And you are not using a one-liner arrow function, since you use braces around the function body. => { }. So try composite => composite.blocked === showBlocked without braces. Commented Sep 22, 2022 at 14:53
  • @fg_st, it would be implicit if you surround it with parenthesis , (composite) => (composite.blocked == showBlocked) Commented Sep 22, 2022 at 14:54

1 Answer 1

2

Your filter function is not returning anything. You should either add a return:

let composites_copy = element.composites.filter( (composite) => {
   return composite.blocked == showBlocked
})

or simplify to:

let composites_copy = element.composites.filter( 
  (composite) => composite.blocked == showBlocked
)
Sign up to request clarification or add additional context in comments.

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.