gpt4 book ai didi

javascript - 如何使用 jQuery Grep 过滤极度嵌套的 JSON

转载 作者:行者123 更新时间:2023-12-03 11:54:08 25 4
gpt4 key购买 nike

我对 JS 还很陌生,不知道如何正确编写 return 。基本上,贷方是一组人,每个人都有项目,每个人都有请求,每个人都有开始和结束日期。当用户输入开始/结束日期进行搜索时,我需要查看 JSON 对象,如果任何项目的请求的开始/结束日期与用户搜索的内容重叠,则该项目需要从结果中排除。

原始贷方 JSON

[
{"email":"test@yahoo.com","inventories":
[
{"name":"2-Person Tent","requests":
[
{"pickupdate":"2015-01-03","returndate":"2015-01-08"},
{"pickupdate":"2015-02-01","returndate":"2015-02-02"}
]
},
{"name":"Sleeping bag","requests":
[
{"pickupdate":"2015-02-01","returndate":"2015-02-02"}
]
},
{"name":"Sleeping bag","requests":[]},
]
},
{"email":"test@gmail.com",,"inventories":[]}
]
...

例如,如果用户搜索pickupdate 2015-01-03直到returndate 2015-01-07,则与请求重叠对于 2 人帐篷,因此 grep 函数的结果应该是:

新的贷方 JSON

[
{"email":"test@yahoo.com","inventories":
[
// 2-Person tent has been removed by grep function
{"name":"Sleeping bag","requests":
[
{"pickupdate":"2015-02-01","returndate":"2015-02-02"}
]
},
{"name":"Sleeping bag","requests":[]},
]
},
{"email":"test@gmail.com",,"inventories":[]}
]
...

我从 @Олег Ильин 那里得到了一个很好的起点,它帮助我弄清楚如何深入到 Request 对象,但是 is_valid 部分不起作用,在 grep 函数之前和之后返回的库存数组完全相同。额外的帮助?

  function FilterLenders() { 
console.log(lenders[0].inventories)
for(var n=0; n<lenders.length; n++){
inventories = lenders[n].inventories
inventories = jQuery.grep(inventories, function (inventory) {
var is_valid;
requests = inventory.requests;
for(var x=0; x<requests.length; x++){
var pickupdate_request = moment(requests[x].pickupdate)
var returndate_request = moment(requests[x].returndate)
var test = (pickupdate_request - returndate_search) * (pickupdate_search - returndate_request)
if(test > 0) {
//dates completely overlap the item that had this request needs to be excluded
is_valid = false;
} else if(test === 0) {
//dates are on the edge of each other, for now the item that had this request can be included
is_valid = true;
} else if(test <0) {
//dates do not overlap at all, item should definitely be included
is_valid = true
}
}
})
}
console.log(lenders[0].inventories) //first object, 2-person tent, should not exist
}

此外,还有另一个想法可以做到这一点,但问题是在这里,它永远不会返回 false,即使我放置了 console.log("false") code>,这会告诉我存在 false

function FilterLenders() { 
console.log(lenders[1].inventories)
for(var n=0; n<lenders.length; n++){
inventories = lenders[n].inventories
inventories = jQuery.grep(inventories, function (inventory) {
function check(inventory) {
requests = inventory.requests;
for(var x=0; x<requests.length; x++){
var pickupdate_request = moment(requests[x].pickupdate)
var returndate_request = moment(requests[x].returndate)
var test = (pickupdate_request - returndate_search) * (pickupdate_search - returndate_request)
if(test > 0) {
//dates completely overlap the item that had this request needs to be excluded
return false;
//altho return doesn't seem to work, if I put a console.log("false") here it will correctly log a false entry...
break;
} else if(test === 0) {
//dates are on the edge of each other, for now the item that had this request can be included
return true;
} else if(test <0) {
//dates do not overlap at all, item should definitely be included
return true;
}
}
}
console.log(check(inventory))
if( check(inventory) ) {
return inventory
}
})
}
console.log(lenders[1].inventories) //object 5 should not exist
}

最佳答案

如果 jquery.grep 解决方案太复杂,您可以使用专门的过滤器,如 json easy filter看这个plunkr

var start = new Date('2015-01-03');
var end = new Date('2015-01-07');
var success = new Jef(data).remove(function (node) {
if (node.has('requests')) {
var requests = node.value.requests;
for (var i = 0; i < requests.length; i++) {
var request = requests[i];
var pick = new Date(request.pickupdate);
var ret = new Date(request.returndate);
if (!((pick < start && ret < start) || (pick > end && ret > end))) {
// pickupdate-returndate overlaps with start-end
return node;
}
}
}
});

alert(JSON.stringify(data, null, 4));

关于javascript - 如何使用 jQuery Grep 过滤极度嵌套的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25678022/

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