2
const Person = [
    {
        firstname: "john",
        lastname: "doe",
        items: [
            {
                visible: true,
                foo: "bar"
            },
            {
                visible: false,
                foo: "bar"
            }
        ]
    },
        {
        firstname: "jane",
        lastname: "doe",
        items: [
            {
                visible: false,
                foo: "bar"
            }
        ]
    },
    {
        firstname: "john",
        lastname: "adam",
        items: [
            {
                visible: true,
                foo: "bar"
            },
            {
                visible: false,
                foo: "bar"
            }
        ]
    },
]

In the above array of Person objects, i have an array of items objects that has a property visible. I'm trying to filter Person object based on the visible field: true or false. The filtered Person object's items array of object will only hold true or false as well. How can I achieve this?

I have tried:

I tried:

const filteredPerson = Person.filter(person => {
    person.items.filter(item => item.visible === true)
})
2
  • 2
    The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a minimal reproducible example. For more information, please see How to Ask and take the tour. Commented Dec 3, 2018 at 5:25
  • Please add your input and expected output. Commented Dec 3, 2018 at 5:26

2 Answers 2

2

Single filter() is enough here. Since the visible property in item is at 0 index you can use the that index:

const Person = [{firstname: "john",lastname: "doe",items: [{visible: true,foo: "bar"}]},{firstname: "jane",lastname: "doe",items: [{visible: false,foo: "bar"}]},{firstname: "john",lastname: "adam",items: [{visible: true,foo: "bar"}]}];

const filteredPerson = Person.filter(person => person.items[0].visible)
console.log(filteredPerson);

If the property index is not known then you have to use some() inside filter(). You can use map() to return the specific property from objects in the array:

const Person = [{firstname: "john",lastname: "doe",items: [{visible: true,foo: "bar"}]},{firstname: "jane",lastname: "doe",items: [{visible: false,foo: "bar"}]},{firstname: "john",lastname: "adam",items: [{visible: true,foo: "bar"}]}];

const filteredPerson = Person.filter(person => person.items.some(s =>s.visible)).map(p => ({items: p.items}));
console.log(filteredPerson);

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

12 Comments

What if its not on the first index?
Thanks. What if I want to only filter the items array based on true or false?
@ZeroDarkThirty, do you mean only to get the item not other property as the result?
Yup just the array of items from each Property object, but still filtered by true/false.
@ZeroDarkThirty, then you have to use map() to return only the items.. Updated the second answer....please check.
|
2

Use Array.some

const Person = [{firstname: "john",lastname: "doe",items: [{visible: true,foo: "bar"}]},{firstname: "jane",lastname: "doe",items: [{visible: false,foo: "bar"}]},{firstname: "john",lastname: "adam",items: [{visible: true,foo: "bar"}]}];
// Filters all those objects in Person array which has atleast one visible item
const result = Person.filter(v => v.items.some(o => o.visible));
console.log(result);

EDIT (Use nested Array.filter)

const Person = [{firstname: "john",lastname: "doe",items: [{visible: true,foo: "bar"},{visible: false,foo: "bar"}]},{firstname: "jane",lastname: "doe",items: [{visible: false,foo: "bar"}]},{firstname: "john",lastname: "adam",items: [{visible: true,foo: "bar"},{visible: false,foo: "bar"}]}];
// Filters all those objects in Person array which has atleast one visible item
const result = Person.filter(v => {
  const filteredItems = v.items.filter(o => o.visible);
  if(filteredItems.length) { // if there is any visible item
    v.items = filteredItems;
    return true;
  }
});
console.log(result);

5 Comments

I've updated my question with another scenario. now items has multiple objects, each with visible either true or false. how can i filter Person now, either by true OR false?
when you have this scenario, some doesn't filter the items array in the filtered Person array that gets returned.
This only works for true items. how do i do it for false?
@ZeroDarkThirty - Do you want to filter only those values where all the items in an object are either true or false?
@ZeroDarkThirty - Which also means that in the above example posted, the resulting array will have only 1 element with item having only 1 entry. As the other 2 elements in array will be ignored as in each of the items array there is a mixture of true and false entries

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.