我正在尝试创建一个新的JSON对象,其中包含此源JSON中的两个项目,其中'date_time'值以'2015-09-21'开头。这应该返回项目160和161(不包括162)。这是JSON:
{
"data":[
{
"item":{
"number":"160",
"name":"Project example phase 2",
"estimated_duration":{
"duration_minutes":0.0
},
"remaining_duration":{
"duration_minutes":0.0
},
"customer":{
"company_name":null,
"id":null
},
"reported_by_customer_contact":{
"name":"",
"id":null
},
"item_type":"features",
"id":160
},
"user":{
"name":"Joe Bloggs",
"id":31
},
"work_log_type":{
"name":"Architecture",
"id":1
},
"work_done":{
"duration_minutes":120.0,
"time_unit":{
"abbreviation":"min",
"name":"Minutes",
"id":1
},
"text":"120 min",
"duration":120.0
},
"id":263,
"description":"Testing",
"date_time":"2015-09-21T13:48:00Z"
},
{
"item":{
"number":"161",
"name":"Project abc",
"estimated_duration":{
"duration_minutes":0.0
},
"remaining_duration":{
"duration_minutes":0.0
},
"customer":{
"company_name":"Universal Exports",
"id":2
},
"reported_by_customer_contact":{
"name":"Mark Zuckerstein",
"id":3
},
"item_type":"incidents",
"id":1
},
"user":{
"name":"Joe Bloggs",
"id":31
},
"work_log_type":{
"name":"Analysis",
"id":2
},
"work_done":{
"duration_minutes":300.0,
"time_unit":{
"abbreviation":"hrs",
"name":"Hours",
"id":2
},
"text":"5 hrs",
"duration":5.0
},
"id":248,
"description":"Stupid coffee cup",
"date_time":"2015-09-21T11:28:00Z"
},
{
"item":{
"number":"162",
"name":"I broke my cup holder",
"estimated_duration":{
"duration_minutes":0.0
},
"remaining_duration":{
"duration_minutes":0.0
},
"customer":{
"company_name":"Universal Exports",
"id":2
},
"reported_by_customer_contact":{
"name":"Mark Zuckerstein",
"id":3
},
"item_type":"incidents",
"id":1
},
"user":{
"name":"Joe Bloggs",
"id":31
},
"work_log_type":{
"name":"Architecture",
"id":1
},
"work_done":{
"duration_minutes":10.0,
"time_unit":{
"abbreviation":"min",
"name":"Minutes",
"id":1
},
"text":"10 min",
"duration":10.0
},
"id":249,
"description":"Trying to fix his cup holder",
"date_time":"2015-09-22T11:33:00Z"
}
],
"metadata":{
"total_count":3,
}
}
我开始玩lodash,但是在过滤了实际时间并试图将'过滤器'与其他lodash功能结合起来之后很快就离开了我的深度。
var filtered = _.filter(object.data, { date_time: "2015-09-21T13:48:00Z"});
非常感谢任何帮助。
答案 0 :(得分:2)
没有必要使用lodash。您可以使用Array.prototype.filter
方法。用于过滤数组的Array.prototype.filter
方法和用于检查特定字符串索引的String.prototype.indexOf
方法。
yourVariable.data.filter(function(el) {
return el.date_time.indexOf('2015-09-21') === 0;
});
答案 1 :(得分:0)
var final = [];
var by = "2015-09-21";
_.map(data.data, function(o){
if (o.date_time.match(by + ".*")) {
final.push(o);
}
});
console.log(final);