Leo*_*ban 136 javascript arrays lodash
我的对象:
[
{
description: 'object1', id: 1
},
{
description: 'object2', id: 2
}
{
description: 'object3', id: 3
}
{
description: 'object4', id: 4
}
]
Run Code Online (Sandbox Code Playgroud)
在我的函数中,我正在传递描述以找到匹配的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
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用lodash的find方法:https://lodash.com/docs#find
但是我的变量delete_id未定义.
更新人们检查这个问题,Ramda是一个很好的库,可以做同样的事情lodash,但在一个更多功能的编程方式:) http://ramdajs.com/0.21.0/docs/
dan*_*y74 183
lodash和ES5
var song = _.find(songs, {id:id});
Run Code Online (Sandbox Code Playgroud)
lodash和ES6
let song = _.find(songs, {id});
Run Code Online (Sandbox Code Playgroud)
https://lodash.com/docs#find上的文档
Fel*_*ing 158
传递给回调的参数是数组的元素之一.数组的元素是表单的对象{description: ..., id: ...}.
var delete_id = _.result(_.find(savedViews, function(obj) {
return obj.description === view;
}), 'id');
Run Code Online (Sandbox Code Playgroud)
您链接到的文档的另一个替代方案(lodash v3):
_.find(savedViews, 'description', view);
Run Code Online (Sandbox Code Playgroud)
Lodash v4:
_.find(savedViews, ['description', view]);
Run Code Online (Sandbox Code Playgroud)
And*_*ndy 28
你知道你可以轻松地做到这一点,不用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);Run Code Online (Sandbox Code Playgroud)
Dan*_*umb 11
使用该find方法,您的回调将传递每个元素的值,如:
{
description: 'object1', id: 1
}
Run Code Online (Sandbox Code Playgroud)
因此,您需要以下代码:
_.find(savedViews, function(o) {
return o.description === view;
})
Run Code Online (Sandbox Code Playgroud)
您不需要Lodash或Ramda或任何其他额外的依赖项。
只需以功能性方式使用ES6 find()函数:
savedViews.find(el => el.description === view)
Run Code Online (Sandbox Code Playgroud)
有时,您需要使用3rd-party库来获取它们附带的所有好东西。但是,通常来说,在不需要依赖项时,请尝试避免它们。依赖项可以:
for this find the given Object in an Array, a basic usage example of _.find
const array =
[
{
description: 'object1', id: 1
},
{
description: 'object2', id: 2
},
{
description: 'object3', id: 3
},
{
description: 'object4', id: 4
}
];
Run Code Online (Sandbox Code Playgroud)
this would work well
q = _.find(array, {id:'4'}); // delete id
console.log(q); // {description: 'object4', id: 4}
Run Code Online (Sandbox Code Playgroud)
_.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.
var delete_id = _(savedViews).where({ description : view }).get('0.id')
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以使用以下内容
import { find } from 'lodash'
Run Code Online (Sandbox Code Playgroud)
然后使用以下命令从列表中返回整个对象(不仅是其键或值):
let match = find(savedViews, { 'ID': 'id to match'});
Run Code Online (Sandbox Code Playgroud)
导入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"}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
273755 次 |
| 最近记录: |