gpt4 book ai didi

jquery - 使用 jquery 嵌套 json 的最小日期

转载 作者:行者123 更新时间:2023-12-01 07:06:23 25 4
gpt4 key购买 nike

我有一个json字符串

{
"name":"experience",
"spans":[
{"start":"2013-01-01","end":"2015-12-31","name":"Medical emergency","className":"gap","color":"red"},
{"start":"2017-03-01","end":"2017-03-16","name":"Test reason","className":"gap","color":"red"},
{"start":"2010-06-01","end":"2012-20-01","name":"Newtech Media","className":"career","color":"green"},
{"start":"2016-01-01","end":"2017-03-01","name":"Exlog Technologies","className":"career","color":"green"},
{"start":"2009-01-01","end":"2009-20-01","name":"Infosys","className":"career","color":"green"}
]
}

我使用

将上面的 json字符串转换为 json对象
var timeObj = $.parseJSON(data);

现在我想要最小开始日期。即来自上面的字符串,它是 2009-01-01

如何从嵌套的 json 对象中获取最小的日期?

我尝试了很多,但没有得到确切的结果。我想有人可以帮助我..

最佳答案

var data = {
"name":"experience",
"spans":[
{"start":"2013-01-01","end":"2015-12-31","name":"Medical emergency","className":"gap","color":"red"},
{"start":"2017-03-01","end":"2017-03-16","name":"Test reason","className":"gap","color":"red"},
{"start":"2010-06-01","end":"2012-20-01","name":"Newtech Media","className":"career","color":"green"},
{"start":"2016-01-01","end":"2017-03-01","name":"Exlog Technologies","className":"career","color":"green"},
{"start":"2009-01-01","end":"2009-20-01","name":"Infosys","className":"career","color":"green"}
]
};

var dateArray = data.spans.map(function(item) {
return new Date(item.start);
});

dateArray.sort(function(a, b) {
return a - b;
});

var lowestDate = dateArray[0];
console.log(lowestDate);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

如果您想保留其格式,请编辑如下

var data = {
"name":"experience",
"spans":[
{"start":"2013-01-01","end":"2015-12-31","name":"Medical emergency","className":"gap","color":"red"},
{"start":"2017-03-01","end":"2017-03-16","name":"Test reason","className":"gap","color":"red"},
{"start":"2010-06-01","end":"2012-20-01","name":"Newtech Media","className":"career","color":"green"},
{"start":"2016-01-01","end":"2017-03-01","name":"Exlog Technologies","className":"career","color":"green"},
{"start":"2009-01-01","end":"2009-20-01","name":"Infosys","className":"career","color":"green"}
]
};

var dateArray = data.spans.map(function(item) {
return item.start;
});

dateArray.sort(function(a, b) {
return new Date(a) - new Date(b);
});

var lowestDate = dateArray[0];
console.log(lowestDate);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

关于jquery - 使用 jquery 嵌套 json 的最小日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42994471/

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