gpt4 book ai didi

javascript - 按 date_time 值开始选择/过滤 JSON

转载 作者:行者123 更新时间:2023-12-02 15:39:04 25 4
gpt4 key购买 nike

我正在尝试创建一个新的 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"});

任何帮助将不胜感激。

最佳答案

不需要使用lodash。您可以使用 Array.prototype.filter方法。 Array.prototype.filter过滤数组的方法和 String.prototype.indexOf检查该特定字符串的索引的方法。

yourVariable.data.filter(function(el) { 
return el.date_time.indexOf('2015-09-21') === 0;
});

关于javascript - 按 date_time 值开始选择/过滤 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32716573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com