[英]Get object from json based on value using Lodash
我有以下json格式
{
"timings": {
"a": "02:57",
"b": "05:19",
"c": "13:22",
"d": "17:39",
"e": "21:26",
"f": "21:26",
"g": "23:40",
"h": "02:47",
"i": "01:22"
}
}
使用lodash和momentjs,如何选择值相同或在当前时间之后的对象。
我尝试了以下
let findTime = (times) => {
const timeNow = moment();
let nextEvent = null;
lodash.find(times, function (value, key) {
const thisTime = moment(value, 'HH:mm');
if (thisTime.isSameOrAfter(timeNow, 'second')) {
console.log(key + " is next");
nextEvent = {
'name': key,
'time': value
};
return;
}
});
return nextEvent;
}
问题是无论如何我都会不断退货。 我发现该函数可以遍历所有条目。
我找不到其他解决方案。
我需要保持时代的秩序。
任何指针...?
不知道您到底在尝试什么。 使用_.find
的理想方法是为匹配return true
(并且不会迭代下一个元素)。 当您从谓词中return true
, _.find
将返回该键的值。
当您从匹配元素的谓词return true
:
_.find
返回您第一次匹配的值
_.findLast
返回最后匹配的值
_.findKey
返回你的第一场比赛的关键
_.findLastKey
返回最后匹配的键
_.filter
以数组的_.filter
返回所有匹配项的值
注意:如果要获取最近的即将到来的时间,则需要在传递给这些lodash函数中的任何一个之前对timings
进行排序。
let findTime = (times) => { const timeNow = moment(); let nextEvent = null; let ret = _.find(times, function (value, key) { const thisTime = moment(value, 'HH:mm'); if (thisTime.isSameOrAfter(timeNow, 'second')) { console.log(key + " is next"); nextEvent = { 'name': key, 'time': value }; return true; } }); //return nextEvent; return ret; // send the return value from lodash function } let times = { "timings": { "a": "02:57", "b": "05:19", "c": "13:22", "d": "17:39", "e": "21:26", "f": "21:26", "g": "23:40", "h": "02:47", "i": "01:22" } } console.log('returned value:', findTime(times.timings));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>
您可以使用Object.entries()
将JSON
迭代为javascript
对象,使用+
运算符和.replace()
将日期转换为整数。 如果condition为true
,则将对象的属性设置为当前属性和值。
const data = { "timings": { "a": "02:57", "b": "05:19", "c": "13:22", "d": "17:39", "e": "21:26", "f": "21:26", "g": "23:40", "h": "02:47", "i": "01:22" } } let res = {}; // let now = `${new Date().toJSON()}`.slice(11, 16); let now = "21:26"; let dateToNumber = str => +str.replace(/:/, ""); for (let [key, prop] of Object.entries(data.timings)) { if (dateToNumber(prop) >= dateToNumber(now)) { // do stuff res[key] = prop; } } console.log(res);
您可以使用lodash#findKey
从对象中查找特定的键。
const findTime = times => {
let mNow = moment();
let name = _.findKey(times, v => moment(v, 'HH:mm').isSameOrAfter(mNow));
var time = times[name];
return time? { name, time }: null;
};
let data = { "timings": { "a": "02:57", "b": "05:19", "c": "13:22", "d": "17:39", "e": "21:26", "f": "21:26", "g": "23:40", "h": "02:47", "i": "01:22" } }; const findTime = times => { let mNow = moment(); let name = _.findKey(times, v => moment(v, 'HH:mm').isSameOrAfter(mNow)); var time = times[name]; return time? { name, time }: null; }; console.log(findTime(data.timings));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.