0

Below is my JSON array and I want to find if contact array has specific value or not.

 data =  [ { _id: 5a67294923aba20014e2fe5a,
            customerLocation: '473 Victoria St, Singapore 198371',
            customerName: 'UNIVERSAL',
            id: 'UNI3L2',
            customerType: 'customer',
            contact:
             [ 'Brian spilak',
               'Brian spilak2',
               'Brian spilak3',
               'Brian spilak4',
               'Brian spilak5' ] } ];

JAVASCRIPT

function findContact(data,con){
           return data.contact.find(item => {
            return item.data.contact == con;
        })
     }    

    console.log('Contact Name :', findContact(data,'Brian spilak2'));
4
  • @joe-tom help than ;) Commented Mar 14, 2018 at 6:03
  • 2
    replace item.data.contact == con; with just item == con; Commented Mar 14, 2018 at 6:04
  • It's not clear what you are trying to find. Will data have more than one customer object? Are you trying to find a particular customer? Or will it always just have one object? Commented Mar 14, 2018 at 6:07
  • @Mark_M No there will be only one customer object and one contact object. I want to find if contact object has specific value or not. Commented Mar 14, 2018 at 6:08

5 Answers 5

2

item doesn't have data.contact property and your data response it an array, so you have to specify an index do work with:

function findContact(data,con){
           return data[0].contact.find(item => {
            return item == con;
        })
     }    
Sign up to request clarification or add additional context in comments.

1 Comment

data is an array. It has no contact property.
0

You can use data.contact for contact data shown

Comments

0

You can use array#find with array#includes to check if a name exists in the contact property of the object.

var data = [ { _id: '5a67294923aba20014e2fe5a', customerLocation: '473 Victoria St, Singapore 198371', customerName: 'UNIVERSAL', id: 'UNI3L2', customerType: 'customer', contact: [ 'Brian spilak', 'Brian spilak2', 'Brian spilak3', 'Brian spilak4', 'Brian spilak5'] } ];
var result = data.find(o => o.contact.includes('Brian spilak2'));
console.log(result);

4 Comments

Why are you using some() rather than just the returned boolean from includes()?
Some is used here to iterate the array.
@HassanImam What if i want to return found object ?
I have updated the solution to return the complete object.
0

This one returns all the objects matching the name criteria in the array.

function filterName(data, srchName) {
   return data.filter((obj) => {
        return obj.contact.indexOf(srchName) !== -1
    })
}

console.log(filterName(data, 'Brian spilak'));

Comments

-1

Use indexOf.Since it is only one object data[0] is used to get the first object, if it is an array of object you can use forEach method

var data = [{
  _id: '5a67294923aba20014e2fe5a',
  customerLocation: '473 Victoria St, Singapore 198371',
  customerName: 'UNIVERSAL',
  id: 'UNI3L2',
  customerType: 'customer',
  contact: ['Brian spilak',
    'Brian spilak2',
    'Brian spilak3',
    'Brian spilak4',
    'Brian spilak5'
  ]
}];

function findContact(data, con) {
  if (data[0].contact.indexOf(con) !== -1) {
    return 'found';
  } else {
    return 'not found';
  }

}

console.log('Contact Name :', findContact(data, 'Brian spilak2'));

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.