使用下面的数据对象,我正在尝试编写一个函数,它接受一个名称(名称是数据中的标签,如本例中的“GD”)和值。我想使用下面在下划线中编写的函数,但是我很难弄清楚如何写出这段代码,因为我一般都是JS的新手。有关如何修改函数foos = soup.select("div.foo")
bars = soup.select("div.bar")
bazs = soup.select("div.baz")
for i in range(len(foos)):
print("{i} contains: {} and {} and {}".format(i=i, foos[i], bars[i], bazs[i]))
的任何输入?
for i in soup.select("li"):
#how would i do this:
foo = child_of("li", "div.foo")????
bar = child_of("li", "div.bar")????
baz = child_of("li", "div.baz")????
答案 0 :(得分:0)
根据文件,
_。findWhere ( list , properties )
查看列表和 返回与列出的所有键值对匹配的第一个值 在属性。
如果未找到匹配项,或者list为空,则返回undefined。
_。findWhere ( publicServicePulitzers ,{ newsroom :“The New York Times”}); => {年:1918年,新闻编辑:“纽约时报”,理由是:“因为它的公共服务在出版这么多官方报告中, 欧洲政治家关于进展的文件和演讲 并且进行了战争。“}
看起来您必须指定属性名称和属性值。在这种情况下,分别是“标签”和“GD”。
试试这个:
var data = [{},{},
{
field:"ran",
indexable:true,
label:"R",
options: [
{},{},
{
category_value:"200",
label:"GD"
},{},{} // why so many initalized empty arrays?
]
},{}
]
function getOption (name, value) {
return _.findWhere(data.options, {label: "GD"}); // returns the whole array
// try var result = _.findWhere(data.options, {label: "GD"});
// then return result.category_value;
}
刚推荐以下网址与某些相关的帖子。它可能对你有用,它不使用额外的库:dynamic access to an array in javascript