我需要一些建议或最佳方法,以使其具有传递的值到数组数据过滤器中。 目前,我有
{"code":100,"message":"Nieprawidłowy kod dostępu","message_code":"invalid_authentication_token"}
如果我通过了html_session("www.xyz.com") %>% jump_to("www.xyz.com/api/v2/table")
&
r = GET("www.xyz.com")
# taking out cookies and manually write it down:
cookies(r)
r2 = GET("www.xyz.com/api/v2/table",
set_cookies(`xxx` = "xxxxxxxxxxx"))
,那么返回数组将是
>>> def org_dest(d):
for k,v in d.items():
date_struc[k]=v
if all(date_struc.values()):
pass
else:
for k,v in date_struc.items():
if not v:
date_struc[k]= "".join([val for val in date_struc.values() if val])
return date_struc
如果我通过了None
返回结果
>>> date_struc = {"originDate": None, "destDate": None}
与srnum类似,在传递时会给出过滤器数组对象结果。
当前
我尝试使用
>>> date_struc = {"originDate": None, "destDate": None}
origin_dest_date_1 = {"originDate": "2019-06-30 23:59:00.000", "destDate": None}
>>> org_dest(origin_dest_date_1) # test-2 output
{'originDate': '2019-06-30 23:59:00.000', 'destDate': '2019-06-30 23:59:00.000'}
>>> origin_dest_date_2 = {"originDate": "2019-06-30 23:59:00.000"}
>>> org_dest(origin_dest_date_2) #test-2 output
{'originDate': '2019-06-30 23:59:00.000', 'destDate': '2019-06-30 23:59:00.000'}
它给出上下文结果,但不可预测。
让我知道过滤数据的最佳方法。 谢谢。
答案 0 :(得分:2)
嗯,我认为您的方法几乎是最好的。但是,您使用_.some
只会在任何项目匹配时给出布尔结果,如果您将其与_.filter
交换,则可以得到所有匹配的项目,也可以将_.find
更改为得到第一个:
let arrayobject = [
{"name": "apple", "type": "juice-material", "srnum": 123234},
{"name": "cranberry", "type": "desert-material", "srnum": 98989},
{"name": "grapes", "type": "wine-material", "srnum": 656565}
]
function filterByValues(collection, whatToSearchFor) {
//separating just for easier readability
let predicate = _.unary(_.partialRight(_.includes, whatToSearchFor));
return _.filter(collection, predicate);
}
//matches a value
console.log(filterByValues(arrayobject, "apple"));
console.log(filterByValues(arrayobject, "wine-material"));
console.log(filterByValues(arrayobject, 98989));
//doesn't match a key
console.log(filterByValues(arrayobject, "name"));
console.log(filterByValues(arrayobject, "type"));
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>
话虽如此,@ georg在评论中指出,您可以使用较少的Lodash功能重新编写此代码,而只需使用_.includes
进行简单的回调-此可能比使用_.partialRight
和_.unary
更可读:
let arrayobject = [
{"name": "apple", "type": "juice-material", "srnum": 123234},
{"name": "cranberry", "type": "desert-material", "srnum": 98989},
{"name": "grapes", "type": "wine-material", "srnum": 656565}
]
function filterByValues(collection, whatToSearchFor) {
//separating just for easier readability
let predicate = item => _.includes(item, whatToSearchFor);
return _.filter(collection, predicate);
}
//matches a value
console.log(filterByValues(arrayobject, "apple"));
console.log(filterByValues(arrayobject, "wine-material"));
console.log(filterByValues(arrayobject, 98989));
//doesn't match a key
console.log(filterByValues(arrayobject, "name"));
console.log(filterByValues(arrayobject, "type"));
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>
答案 1 :(得分:0)
您可以使用arrayobject.filter()
过滤所需的道具
let arrayobject = [
{"name": "apple", "type": "juice-material", "srnum": 123234},
{"name": "cranberry", "type": "desert-material", "srnum": 98989},
{"name": "grapes", "type": "wine-material", "srnum":656565}]
var res = arrayobject.filter(x => x.name == "apple");
console.log(res);
var res2 = arrayobject.filter(x => x.type == "wine-material");
console.log(res2);
答案 2 :(得分:0)
可以按以下方式应用过滤器:
function filterItems(items, searchVal) {
return items.filter((item) => Object.values(item).includes(searchVal));
}
let arrayobject = [
{ "name": "apple", "type": "juice-material", "srnum": 123234 },
{ "name": "cranberry", "type": "desert-material", "srnum": 98989 },
{ "name": "grapes", "type": "wine-material", "srnum": 656565 }]
console.log("Filtered by name: ", filterItems(arrayobject, "apple"));
console.log("Filtered by type: ", filterItems(arrayobject, "desert-material"));
console.log("Filtered by srnum: ", filterItems(arrayobject, 656565));
答案 3 :(得分:0)
您可以将 flatMap 与所需数组一起使用。 您只需提供要查找的属性即可。
您的情况是
let arrayobject = [
{"name": "apple", "type": "juice-material", "srnum": 123234},
{"name": "cranberry", "type": "desert-material", "srnum": 98989},
{"name": "grapes", "type": "wine-material", "srnum":656565}]
var filteredData = getFilterData(arrayobject, 656565);
console.log(filteredData);
function getFilterData(array, searchValue){
return array.flatMap(obj => obj)
.find(({ name,type,srnum }) => (name === searchValue || type == searchValue)
|| srnum == searchValue);
}
答案 4 :(得分:0)
您可以在数组对象上使用filter()
。 filter()
方法创建一个新数组,其中所有元素都通过了由提供的功能实现的测试。
arrayobject.filter(data => {
return (data.name && data.name.includes(value)) ||
(data.type && data.type.includes(value)) ||
(data.srnum && data.srnum.includes(value))
})
答案 5 :(得分:0)
如果要从数组中筛选出包含键中任何值的对象,则可以直接在每个对象中使用_.includes从数组中筛选( _.filter)。如果需要查看一组特定的键,只需在将对象传递给include之前使用_.pick
。
let array = [
{id: 1, x: 'XX1', y: 'YY1', z: 'ZZ1'},
{id: 2, x: 'XX1', y: 'YY2', z: 'ZZ2'},
{id: 3, x: 'ZZ2', y: 'YY3', z: 'ZZ1'},
],
filterByValue = (a, v) => _.filter(a, e => _.includes(e, v)),
filterByValueInKeys = (a, v, keys) => _.filter(a, e => _.includes(_.pick(e, keys), v));
console.log('ZZ1: ', filterByValue(array, 'ZZ1'));
console.log('ZZ2: ', filterByValue(array, 'ZZ2'));
console.log('ZZ1 within x,y: ', filterByValueInKeys(array, 'ZZ1', ['x', 'y']));
console.log('ZZ2 within x,y: ', filterByValueInKeys(array, 'ZZ2', ['x', 'y']));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>