我有一个像这样的对象数组:
result = [{
email: 'jaiswal.shubham84@gmail.com',
profile: './image/jaiswal.shubham84@gmail.com/6.jpg'
},
{
email: 'sj1303058@gmail.com',
profile: './image/sj1303058@gmail.com/5.jpg'
}
]
如何根据result
在email
中找到正确的对象?
答案 0 :(得分:2)
{{1}}
答案 1 :(得分:0)
我认为@Gabriel Bleu很好地回答了这个问题。我只想补充一点:如果你想要所有符合条件的对象,请使用filter代替find:
result = [{
email: 'jaiswal.shubham84@gmail.com',
profile: './image/jaiswal.shubham84@gmail.com/6.jpg'
},
{
email: 'sj1303058@gmail.com',
profile: './image/sj1303058@gmail.com/5.jpg'
},{
email: 'jaiswal.shubham84@gmail.com',
profile: 'heyehye'
},
{
email: 'hahahahaha@gmail.com',
profile: 'hahahaha'
}
]
//every matching email. Returns an array of all matching (even if only one)
const filtered = result.filter(r => r.email === 'jaiswal.shubham84@gmail.com')
//only one. Will be the first one found so returns an object
const found = result.find(r => r.email === 'jaiswal.shubham84@gmail.com')
console.log("filtered : " + JSON.stringify(filtered))
console.log("found : " + JSON.stringify(found))
答案 2 :(得分:0)
如果以前没有安装lodash那么
var lodash = require(“lodash”);
function getobject(array,keyvalue)
{
var obj = lodash.filter(array, { 'email': keyvalue } );
return picked[0].profile;
}