下划线JS通过ID检索对象
Underscore JS to retrieve object by ID
我有一个输入,我想通过 ID 查找对象。目前我正在 returning 这两个对象,但我想要的是,如果我搜索“01”,我只会 return 第一个对象。我已经尝试使用下划线 _.map 来执行此操作,但它没有给出我想要的结果。
var getById = function() {
var deferred = Q.defer(),
result;
result = items;
if (!result) {
deferred.reject('item not found');
} else {
deferred.resolve(result);
}
return deferred.promise;
};
JSON:
[{
"id": "01",
"name": "test1",
"orderItems": [
{
"productNumber": "TESTa",
"quantity": 2,
},
{
"productNumber": "TESTb",
"quantity": 4,
},
{
"productNumber": "TESTc",
"quantity": 6,
}
]
},{
"id": "02",
"name": "test2",
"orderItems": [
{
"productNumber": "TESTe",
"quantity": 2,
},
{
"productNumber": "TESTf",
"quantity": 7,
},
{
"productNumber": "TESTg",
"quantity": 6,
}
]
}]
您可以使用_.filter()
Looks through each value in the list, returning an array of all the values that pass a truth test (predicate).
result = _.filter(items, function(item){
return item.id == '01';
});
或者,_.findWhere
Looks through the list and returns the first value that matches all of the key-value pairs listed in properties.
result = _.findWhere(items, {id : '01'});
var result = _.find(myItems, function(item) { return item.id === '01'; }
如果找到符合条件的单个项目,请使用 _.find()
It looks through each value in the list, returning the first one that
passes a truth test
var _exist = _.find(_items, function (item) {
return item.id == your_id;
});
如果找到所有符合条件的项目,请使用 _.filter()
It looks through each value in the list, returning an array of all the
values that pass a truth test
var _exist = _.filter(_items, function (item) {
return item.id == your_id;
});
在此处获取完整文档:
http://underscorejs.org/
我有一个输入,我想通过 ID 查找对象。目前我正在 returning 这两个对象,但我想要的是,如果我搜索“01”,我只会 return 第一个对象。我已经尝试使用下划线 _.map 来执行此操作,但它没有给出我想要的结果。
var getById = function() {
var deferred = Q.defer(),
result;
result = items;
if (!result) {
deferred.reject('item not found');
} else {
deferred.resolve(result);
}
return deferred.promise;
};
JSON:
[{
"id": "01",
"name": "test1",
"orderItems": [
{
"productNumber": "TESTa",
"quantity": 2,
},
{
"productNumber": "TESTb",
"quantity": 4,
},
{
"productNumber": "TESTc",
"quantity": 6,
}
]
},{
"id": "02",
"name": "test2",
"orderItems": [
{
"productNumber": "TESTe",
"quantity": 2,
},
{
"productNumber": "TESTf",
"quantity": 7,
},
{
"productNumber": "TESTg",
"quantity": 6,
}
]
}]
您可以使用_.filter()
Looks through each value in the list, returning an array of all the values that pass a truth test (predicate).
result = _.filter(items, function(item){
return item.id == '01';
});
或者,_.findWhere
Looks through the list and returns the first value that matches all of the key-value pairs listed in properties.
result = _.findWhere(items, {id : '01'});
var result = _.find(myItems, function(item) { return item.id === '01'; }
如果找到符合条件的单个项目,请使用 _.find()
It looks through each value in the list, returning the first one that passes a truth test
var _exist = _.find(_items, function (item) {
return item.id == your_id;
});
如果找到所有符合条件的项目,请使用 _.filter()
It looks through each value in the list, returning an array of all the values that pass a truth test
var _exist = _.filter(_items, function (item) {
return item.id == your_id;
});
在此处获取完整文档: http://underscorejs.org/