0

I am trying to filter an Array based on the user name and show It but when I try to filter it doesn't show any data. User Structure Is like this

     buyer: [{
              address: ""
              contactNo: ""
              email: "[email protected]"
              name: "user"}]
    cutomerContact: "3058989778"
    orderDate: "2019-07-31T12:10:11.818Z"
    orderType: "general"

and my code to filter array is

this.userOrders = this.filteredOrders.filter(x => x.buyer = x.buyer.name == this.userName)

I have no Idea how to filter a nested data any Help would be appreciated

3
  • replace this.filteredOrders.filter(x => x.buyer = x.buyer.name == this.userName) with this.filteredOrders.filter(x => x.buyer.name == this.userName) Commented Aug 16, 2019 at 8:55
  • @Kinjal I have already tried this it gives nothing but an empty Array Commented Aug 16, 2019 at 8:59
  • if there will always be only one object inside buyer then you can try this.filteredOrders.filter(x => x.buyer[0].name == this.userName) else you have to loop over buyer inside filter Commented Aug 16, 2019 at 9:01

2 Answers 2

2

User filter to filter the main array and some function to check the condition.

The some() method executes the function once for each element present in the array:

  • If it finds an array element where the function returns a true value, some() returns true (and does not check the remaining values)
  • Otherwise it returns false

  name = 'user';
  
  filterData = [
                  {
                    "buyer": [
                      {
                        "address": "",
                        "contactNo": "",
                        "email": "[email protected]",
                        "name": "user"
                      }
                    ],
                    "cutomerContact": "3058989778",
                    "orderDate": "2019-07-31T12:10:11.818Z",
                    "orderType": "general"
                  },
                  {
                    "buyer": [
                      {
                        "address": "",
                        "contactNo": "",
                        "email": "[email protected]",
                        "name": "test"
                      }
                    ],
                    "cutomerContact": "3058989778",
                    "orderDate": "2019-07-31T12:10:11.818Z",
                    "orderType": "general"
                  }
                ]
                
                
  let result = this.filterData.filter( (x) => {
    return x.buyer.some(y => y.name == name)
  })
  
  console.log(result)

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

Comments

0

Try the following

DEMO

let testArray =  [
  {
    "name": "SO",
    "buyer": [
      {
        "address": "",
        "contactNo": "",
        "email": "[email protected]",
        "name": "user"
      }
    ],
    "cutomerContact": "3058989778",
    "orderDate": "2019-07-31T12:10:11.818Z",
    "orderType": "general"
  }
];


let filteredArray = testArray.filter((element) => element.buyer.some((subElement) => subElement.name == 'user')); 
  
console.log(filteredArray);

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.