如何按条件提取 json 元素
How to extract a json element by condition?
我有一个JSON
对象,如下所示:
"links" : [ {
"rel" : "first",
"href" : "http://localhost:8080/first"
}, {
"rel" : "self",
"href" : "http://localhost:8080/self"
}, {
"rel" : "next",
"href" : "http://localhost:8080/next"
}];
现在我想获取rel = "next"
的href
网址。问题:我怎样才能用javascript得到这个?
在Java中,我会遍历数组并创建一个逆HashMap<Rel, Href>
,调用map.get("next");
。
但是在JS中是如何完成的呢?
这应该适合你。
var safe = {
"links": [{
"rel": "first",
"href": "http://localhost:8080/first"
}, {
"rel": "self",
"href": "http://localhost:8080/self"
}, {
"rel": "next",
"href": "http://localhost:8080/next"
}]
};
var i,
l = safe.links.length;
for (i = 0; i < l; i += 1) {
if (safe.links[i].rel === 'next') {
console.log(safe.links[i].href);
}
}
相关文章:
- 如何在html angular中显示嵌套的json元素
- 如何访问JSON元素
- 如何使用javascript获取json元素
- JSON 元素未显示
- JQuery/Javascript - 遍历值之后的所有 json 元素
- 遍历django模板中的json元素
- 在不知道名称的情况下检索json元素
- 主干模型通过ID获取JSON元素
- ExtJS DataView只显示最后一个JSON元素
- 在 AngularJS 中选择输入选择不同的子 JSON 元素
- 获取 json 元素的标签及其 id
- 查找 JSON 元素的索引号
- 将 JSON 元素括在各个列表中
- 如何按条件提取 json 元素
- 使用 Javascript 访问 JSON 元素
- 如何在没有双引号的情况下获取 JSON 元素的值
- jQuery从数组扩展一个json元素
- AngularJS-无法检索json元素
- 在表中添加JSON元素
- 使用javascript的基于日期和时间的json元素