1

I have an object array like this:

result = [{
        email: '[email protected]',
        profile: './image/[email protected]/6.jpg'
    },
    {
        email: '[email protected]',
        profile: './image/[email protected]/5.jpg'
    }
]

How can I find the right object in result based on email?

2
  • What you exactly want give some more details Commented Nov 2, 2017 at 10:04
  • i have an array of object and structure of each object is like {email:'somevalue' , profile:'someothervalue'}, i simple want to find the object from the array using email as a key. Commented Nov 13, 2017 at 14:19

3 Answers 3

2

const result = [
  {
    email: '[email protected]',
    profile: './image/[email protected]/6.jpg'
  },
  {
    email: '[email protected]',
    profile: './image/[email protected]/5.jpg'
  }
];

const jaiswal = result.find(it => it.email === '[email protected]');
console.log(jaiswal);

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

Comments

0

I think @Gabriel Bleu answered it pretty well. I would just add this: if you want all the object matching your condition, use filter instead for find:

result = [{
        email: '[email protected]',
        profile: './image/[email protected]/6.jpg'
    },
    {
        email: '[email protected]',
        profile: './image/[email protected]/5.jpg'
    },{
        email: '[email protected]',
        profile: 'heyehye'
    },
    {
        email: '[email protected]',
        profile: 'hahahaha'
    }
]

//every matching email. Returns an array of all matching (even if only one)
const filtered = result.filter(r => r.email === '[email protected]')
//only one. Will be the first one found so returns an object
const found = result.find(r => r.email === '[email protected]')

console.log("filtered : " + JSON.stringify(filtered))
console.log("found : " + JSON.stringify(found))

Comments

0

npm the lodash if not installed previously

var lodash=require("lodash");

function getobject(array,keyvalue)
{

  var obj = lodash.filter(array, { 'email': keyvalue } );

  return picked[0].profile;
}

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.