如何从对象获取数组
How to get an array from an object
{
"nextPointer": 7,
"tagName": "tech",
"newsList": [{
"newsID": 43,
"title": "This is a title",
"description": "This is a demo description",
"tagList": ["tech", "google", "all"]
}, {
"newsID": 42,
"title": "This is a title no 2",
"description": "This is a demo description no 2",
"tagList": ["tech", "all"]
}]
}
如何获取我的标签列表数据,这是一个数组。
假设此对象分配了一个变量:
var jsonObj = {
"nextPointer": 7,
"tagName": "tech",
"newsList":
[{
"newsID": 43,
"title": "This is a title",
"description": "This is a demo description",
"tagList": ["tech", "google", "all"]
}, {
"newsID": 42,
"title": "This is a title no 2",
"description": "This is a demo description no 2",
"tagList": ["tech", "all"]
}]
};
var tagList = [];
for(var i=0; i<json.newsList.length; i++) {
tagList.push(json[i].tagList);
}
在这里,你会得到一个数组,如下所示:
[["tech", "google", "all"],["tech", "all"]]
试试这个
var data = {
"nextPointer": 7,
"tagName": "tech",
"newsList": [{
"newsID": 43,
"title": "This is a title",
"description": "This is a demo description",
"tagList": ["tech", "google", "all"]
}, {
"newsID": 42,
"title": "This is a title no 2",
"description": "This is a demo description no 2",
"tagList": ["tech", "all"]
}]
}
using jquery
var res = $.map(data.newsList, function(v ,i) {
return v.tagList;
});
using plain javascript
var res = new Array();
for(var i=0; i<data.newsList.length; i++) {
res[i] = data[i].tagList;
}
console.log(res);
小提琴
试试
var data = {
"nextPointer": 7,
"tagName": "tech",
"newsList": [{
"newsID": 43,
"title": "This is a title",
"description": "This is a demo description",
"tagList": ["tech", "google", "all"]
}, {
"newsID": 42,
"title": "This is a title no 2",
"description": "This is a demo description no 2",
"tagList": ["tech", "all"]
}]
}
var allTagList = [];
for (var i in data.newsList) {
allTagList.push(data.newsList[i].tagList);
}
演示
相关文章:
- 获取数组的总值
- 按最大值获取数组索引
- React.js如何从js文件中获取数组
- TinyMCE获取数组数据
- 如何获取数组详细信息另一页
- 使用javascript从节点属性获取数组值
- 使用JavaScript获取数组中表单的所有控件
- 获取数组的长度-打印不正确的值
- 正在从JSON对象获取数组
- 什么'获取数组的已定义部分的最快方法是什么?JS
- 如何使用angular指令从html中获取数组
- 如何获取数组中数字的最大出现次数
- 根据优先级获取数组的值
- JAVASCRIPT:获取数组中的值
- 获取数组的元素,无论索引的大小如何
- JavaScript:在XML中搜索节点并从该节点获取数组
- 无法获取数组的长度
- 在数字索引循环中获取数组项的键
- 如何使用JavaScript或jQuery获取数组中选定的optgroup和选项
- Javascript-函数获取数组元素参数未定义/为null