Filter according field on array object

274 Views Asked by At

I have an object is data like this :

Original data

1

const data = { "title": "sunny", "members": [ { "fullname": "rozo", "customers": [ { "name": "test11", "createdAt": 3 }, { "name": "test12", "createdAt": 4 }, { "name": "test13", "createdAt": 5 } ] }, { "fullname": "luffy", "customers": [ { "name": "test6", "createdAt": 9 }, { "name": "test7", "createdAt": 10 }, { "name": "test8", "createdAt": 11 }, ] } ] }

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?

3

There are 3 best solutions below

2
Harshan Morawaka On
const filteredData = data.members.map(member => {
    if(member.customers) {    
      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

3
Keyacom On

Use Array.prototype.filter(). Define a named function and pass its name (NOT call) to filter():

const memberFilter = e =>
  e.createdAt >= 4 &&
  e.createdAt <= 10;

const filteredMembers = data.members.map(x =>
  x.customers = x.customers.filter(memberFilter)
);

data.members = filteredMembers;

Array.prototype.forEach() will do the thing in the function to the array in-place and return undefined, while .map() will copy the array instead and return the copy.

0
Alexandre Guertin On

You could try this solution:

const data = {
  title: 'sunny',
  members: [
    {
      fullname: 'rozo',
      customers: [
        { name: 'test11', createdAt: 3 },
        { name: 'test12', createdAt: 4 },
        { name: 'test13', createdAt: 5 }]
    },
    {
      fullname: 'luffy',
      customers: [
        { name: 'test6', createdAt: 9 },
        { name: 'test7', createdAt: 10 },
        { name: 'test8', createdAt: 11 }
      ]
    }
  ]
};

const filteredData = {
  /**
   * MDN documentation on destructuring assignment operator
   * ------------------------------------------------
   * The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
   *
   * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
   * ------------------------------------------------
   */
  ...data,
  /**
   * MDN documentation on Array.prototype.map()
   * ------------------------------------------------
   * The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.
   *
   * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
   * ------------------------------------------------
   */
  members: data.members.map(member => ({
    ...member,
    /**
     * MDN documentation on Array.prototype.filter()
     * ------------------------------------------------
     * The filter() method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.
     *
     * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
     * ------------------------------------------------
     */
    customers: member.customers.filter(customer => customer.createdAt >= 4 && customer.createdAt <= 10)
  }))
};

console.log(filteredData);

Output

{
  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 }
      ]
    }
  ]
}