Home > Mobile >  Filter according field on array object
Filter according field on array object

Time:08-19

I have an object is data like this :

Original data

1

I want to filter all customers have create greate than 4 and less than 10, and want to returned result like this :

const data = {
    "title": "sunny",
    "members": [
      {
        "fullname": "rozo",
        "customers": [

          {
            "name": "test12",
            "createdAt": 4
          },
          {
            "name": "test13",
            "createdAt": 5
          }
        ]
      },
      {
        "fullname": "luffy",
        "customers": [
          {
            "name": "test6",
            "createdAt": 9
          },
          {
            "name": "test7",
            "createdAt": 10
          },
        ]
      }
    ]
}

Can anyone help me solve this problem?

CodePudding user response:

const filteredData = data.members.map(member => {
  return member.customers(customer => {
  if(customer.createdAt >= 4 && customer.createdAt <= 10)
  return customer;
})
})

filteredData would be your final output let me know if you getting any problem

CodePudding user response:

you can use filter method to filter data

const filteredMembers = data.members.filter(member => 
  customer.createdAt >= 4 && customer.createdAt <= 10
)
  • Related