Javascript 如何使用 lodash 从 Array 中查找并返回一个对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31054021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use lodash to find and return an object from Array?
提问by Leon Gaban
My objects:
我的对象:
[
{
description: 'object1', id: 1
},
{
description: 'object2', id: 2
}
{
description: 'object3', id: 3
}
{
description: 'object4', id: 4
}
]
In my function below I'm passing in the description to find the matching ID:
在下面的函数中,我传入描述以查找匹配的 ID:
function pluckSavedView(action, view) {
console.log('action: ', action);
console.log('pluckSavedView: ', view); // view = 'object1'
var savedViews = retrieveSavedViews();
console.log('savedViews: ', savedViews);
if (action === 'delete') {
var delete_id = _.result(_.find(savedViews, function(description) {
return description === view;
}), 'id');
console.log('delete_id: ', delete_id); // should be '1', but is undefined
}
}
I'm trying to use lodash's find method: https://lodash.com/docs#find
我正在尝试使用 lodash 的 find 方法:https://lodash.com/docs#find
However my variable delete_id
is coming out undefined.
但是我的变量delete_id
未定义。
Updatefor people checking this question out, Ramda is a nice library to do the same thing lodash does, but in a more functional programming way :) http://ramdajs.com/0.21.0/docs/
为检查这个问题的人更新,Ramda 是一个很好的库,可以做同样的事情 lodash,但以更函数式的编程方式:) http://ramdajs.com/0.21.0/docs/
回答by danday74
lodash and ES5
lodash 和 ES5
var song = _.find(songs, {id:id});
lodash and ES6
lodash 和 ES6
let song = _.find(songs, {id});
docs at https://lodash.com/docs#find
回答by Felix Kling
The argument passed to the callback is one of the elements of the array. The elements of your array are objects of the form {description: ..., id: ...}
.
传递给回调的参数是数组的元素之一。数组的元素是形式的对象{description: ..., id: ...}
。
var delete_id = _.result(_.find(savedViews, function(obj) {
return obj.description === view;
}), 'id');
Yet another alternative from the docs you linked to (lodash v3):
您链接到的文档中的另一种选择(lodash v3):
_.find(savedViews, 'description', view);
Lodash v4:
Lodash v4:
_.find(savedViews, ['description', view]);
回答by Andy
You can do this easily in vanilla JS:
您可以在 vanilla JS 中轻松完成此操作:
Using find
使用 find
const savedViews = [{"description":"object1","id":1},{"description":"object2","id":2},{"description":"object3","id":3},{"description":"object4","id":4}];
const view = 'object2';
const delete_id = savedViews.find(obj => {
return obj.description === view;
}).id;
console.log(delete_id);
Using filter
(original answer)
使用filter
(原始答案)
const savedViews = [{"description":"object1","id":1},{"description":"object2","id":2},{"description":"object3","id":3},{"description":"object4","id":4}];
const view = 'object2';
const delete_id = savedViews.filter(function (el) {
return el.description === view;
})[0].id;
console.log(delete_id);
回答by Dancrumb
With the find
method, your callback is going to be passed the value of each element, like:
使用该find
方法,您的回调将传递每个元素的值,例如:
{
description: 'object1', id: 1
}
Thus, you want code like:
因此,您需要如下代码:
_.find(savedViews, function(o) {
return o.description === view;
})
回答by Afaq Ahmed Khan
for this find the given Object in an Array, a basic usage example of _.find
为此在数组中找到给定的对象,_.find 的基本用法示例
const array =
[
{
description: 'object1', id: 1
},
{
description: 'object2', id: 2
},
{
description: 'object3', id: 3
},
{
description: 'object4', id: 4
}
];
this would work well
这会很好用
q = _.find(array, {id:'4'}); // delete id
console.log(q); // {description: 'object4', id: 4}
_.find will help with returning an element in an array, rather than it's index. So if you have an array of objects and you want to find a single object in the array by a certain key value pare _.find is the right tools for the job.
_.find 将有助于返回数组中的元素,而不是它的索引。因此,如果您有一个对象数组,并且想通过某个键值在数组中查找单个对象,那么 pare _.find 是适合该工作的工具。
回答by xeiton
You don't need Lodash or Ramda or any other extra dependency.
您不需要 Lodash 或 Ramda 或任何其他额外的依赖项。
Just use the ES6 find() function in a functional way:
只需以函数方式使用 ES6 find() 函数:
savedViews.find(el => el.description === view)
Sometimes you need to use 3rd-party libraries to get all the goodies that come with them. However, generally speaking, try avoiding dependencies when you don't need them. Dependencies can:
有时您需要使用 3rd-party 库来获取它们附带的所有好东西。然而,一般来说,当你不需要它们时尽量避免依赖。依赖可以:
- bloat your bundled code size,
- you will have to keep them up to date,
- and they can introduce bugs or security risks
- 膨胀你的捆绑代码大小,
- 你必须让它们保持最新状态,
- 他们可能会引入错误或安全风险
回答by Bican M. Valeriu
You can use the following
您可以使用以下
import { find } from 'lodash'
Then to return the entire object (not only its key or value) from the list with the following:
然后使用以下命令从列表中返回整个对象(不仅是它的键或值):
let match = find(savedViews, { 'ID': 'id to match'});
回答by robertklep
var delete_id = _(savedViews).where({ description : view }).get('0.id')
回答by ABHIJEET KHIRE
Import lodash
using
导入lodash
使用
$ npm i --save lodash
$ npm i --save lodash
var _ = require('lodash');
var objArrayList =
[
{ name: "user1"},
{ name: "user2"},
{ name: "user2"}
];
var Obj = _.find(objArrayList, { name: "user2" });
// Obj ==> { name: "user2"}
回答by Atchutha rama reddy Karri
Fetch id basing on name
根据名称获取id
{
"roles": [
{
"id": 1,
"name": "admin",
},
{
"id": 3,
"name": "manager",
}
]
}
fetchIdBasingOnRole() {
const self = this;
if (this.employee.roles) {
var roleid = _.result(
_.find(this.getRoles, function(obj) {
return obj.name === self.employee.roles;
}),
"id"
);
}
return roleid;
},