0

I am trying to search an array for query string on a json object. The returned fields are an array of filtered results. Currently, the list is returning the name field but not the number field.

      computed: {
        search: function () {
          let self = this
          let filtered = []

          filtered = self.jcontacts.filter(function(contact){
            return  contact.firstname.toLowerCase().indexOf(self.query.toLowerCase())>=0 ||
                    contact.lastname.toLowerCase().indexOf(self.query.toLowerCase())>=0;
                    contact.email.toLowerCase().indexOf(self.query.toLowerCase()) >=0 ||
                    contact.phonenumber.toLowerCase().indexOf(self.query.toLowerCase()) >=0;
            }
          );
          return this.contacts = filtered
        }
      },
    }

The filtered method in the search method is not showing the number. An example of the json is below:

[ { "id": 1, "phoneNumber": [ "3908902" ], "email": [ "[email protected]" ], "firstname": "Jamie", "lastname": "Fox" }]

1
  • 2
    your email and phone number are inside array, try changing code to contact.email[0].toLowerCase() and contact.phonenumber[0].toLowerCase(), this should work Commented May 15, 2019 at 10:43

2 Answers 2

2
  1. Beware of case phoneNumber != phonenumber
  2. phoneNumber is stored as array not string, so you cant look for it like that (use .includes() function for example)
  3. For code formatting consider to store self.query.toLowerCase() as variable or another computed property
Sign up to request clarification or add additional context in comments.

Comments

0

It is a typo. Check out field phoneNumber is filtered as phonenumber.

It is an array so you can do it as,

contact.phoneNumber.forEach( number => { 
   if(number.toLowerCase().indexOf(self.query.toLowerCase()) >=0) 
      return true;
});

I think similarly you can write it down for email as well because it is also an array.

1 Comment

this should be a comment and not an answer

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.