Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Lodash过滤以返回对象而不是长度为1的数组_Javascript_Arrays_Object_Lodash - Fatal编程技术网

Javascript Lodash过滤以返回对象而不是长度为1的数组

Javascript Lodash过滤以返回对象而不是长度为1的数组,javascript,arrays,object,lodash,Javascript,Arrays,Object,Lodash,我正在使用以下代码筛选数组: var filteredValues = _.filter(arrayOfObjects, function(obj) { return obj.id === id; }); 以下是我如何尝试获得第一个结果: console.log('id', filteredValues[0].id); 我知道如何使用链接,但我忘记了我可以使用哪个lodash函数,因此找到的第一个对象将被分配给filteredValues 我可以使用下面的代码,但它看起来太简单了。我

我正在使用以下代码筛选数组:

var filteredValues = _.filter(arrayOfObjects, function(obj) {
    return obj.id === id;
});
以下是我如何尝试获得第一个结果:

console.log('id', filteredValues[0].id);
我知道如何使用链接,但我忘记了我可以使用哪个lodash函数,因此找到的第一个对象将被分配给
filteredValues

我可以使用下面的代码,但它看起来太简单了。我想要一个完整的洛达斯解决方案

var cleanedfilteredValues = {};
cleanedfilteredValues = filteredValues[0];

在Amadan的帮助下,这就是我现在使用的代码

var filteredValues = _.find(arrayOfObjects, function(obj) {
    return obj.id === id;
});

谢谢

使用严格相等运算符比较集合项属性值时,可以使用将对象传递到
find()
,使代码更小:

var filteredValues = _.find(arrayOfObjects, { id: id });