0

I try to filter the hole Object (objB) out of an Array, which is matching a Value (titleB2) of a Property.

const array = [
  {
    name: 'objA',
    subArray: []
  }, {
    name: 'objB',
    subArray: [
      {
        title: 'titleB'
      }, {
        title: 'titleB2'
      }, {
        title: 'titleB3'
      }
    ]
  }, {
    name: 'objC',
    subArray: [
      {
        title: 'titleC'
      }, {
        title: 'titleC2'
      }, {
        title: 'titleC3'
      }
    ]
  }, {
    name: 'objD',
    subArray: []
  }
]

const filterArray = array.filter(a => a.subArray.length > 0);
console.log(filterArray);
// Output: objB + objC

const resArray = filterArray.filter(a => a.subArray.filter(f => f.title === 'titleB2'));
console.log(resArray);
// Output: objB + objC

I guess, I do something logically wrong. But what exactly?

I need the output

{
  name: 'objB',
  subArray: [
    {
      title: 'titleB'
    }, {
      title: 'titleB2'
    }, {
      title: 'titleB3'
    }
  ]
}

I can imagine I use the filter filterArray.filter() wrong, because it already filtered? But I do this because I have multiple arrays in one array. Well, I am not realy sure, to be honest.

But what I dont understand is, why can I use the condition a => a.subArray.length > 0? but not f => f.title === 'titleB2'

2 Answers 2

1

To find objects with a particular title in the subarray you can use some() on the subarray, which will return true if one of the items in the subarray matches your condition (in this case the title):

const array = [{name: 'objA',subArray: []}, {name: 'objB',subArray: [{title: 'titleB'}, {title: 'titleB2'}, {title: 'titleB3'}]}, {name: 'objC',subArray: [{title: 'titleC'}, {title: 'titleC2'}, {title: 'titleC3'}]}, {name: 'objD',subArray: []}]

let found = array.filter(item => item.subArray.some(sub => sub.title === 'titleB2'))
console.log(found)

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

3 Comments

Thank you very much! This really helped me
No need to thank people @equaliser — that's what upvotes and selecting the correct answer are for.
I know, but I mustnt: "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." Thats why i mention that, so you know that I am really thankful :)
0

Use some() instead of filter() on subarray. It returns a boolean.

const resArray = array.filter(a => a.subArray.some(f => f.title === 'titleB2'));
console.log(resArray);
// Output: objB + objC
<script>

const array = [
  {
    name: 'objA',
    subArray: []
  }, {
    name: 'objB',
    subArray: [
      {
        title: 'titleB'
      }, {
        title: 'titleB2'
      }, {
        title: 'titleB3'
      }
    ]
  }, {
    name: 'objC',
    subArray: [
      {
        title: 'titleC'
      }, {
        title: 'titleC2'
      }, {
        title: 'titleC3'
      }
    ]
  }, {
    name: 'objD',
    subArray: []
  }
]
</script>

1 Comment

wow, ok thanks that clears the thing out for me. Thank you very much! :D

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.