如何解决仅从具有特定标志的json返回条目
| 我正在尝试从json获取结果。说我有20种水果类型,但是结果中只有3种水果类型具有price=free
。我怎样才能只返回price = free结果?
这就是我要返回结果的方式。
fruits.payload.entries[count].name
我想知道有ѭ0的水果的名字
解决方法
$(results).filter(function(){
return this.price == \"free\"
});
,您可能需要尝试json:select()— \“ JSON的类似CSS的选择器\” http://jsonselect.org/
,如果您与之交谈的服务不支持该查询,则您必须自行过滤掉它们。
为了完整起见,这里是none框架样式循环:
var i,len,a,freeOnly = [],item;
a = fruits.payload.entries;
for( i = 0,len = a.length; i < len; i ++){
item = a[i];
if( item.price === \'free\' ){
freeOnly.push(item);
}
}
对于免费物品名称,您可以执行以下操作:
function freeItemNames(items){
var i,freeNames = [],item;
for( i = 0,len = items.length; i < len; i ++){
item = items[i];
if( item.price === \'free\' ){
freeNames.push(item.name);
}
}
return freeNames;
}
// call like this
freeItemNames(fruits.payload.entries);
// should output something like
[\"Free fruit 1\",\"Fruity two\"]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。