jquery - 如何使用jquery从json数组中获取第一列

标签 jquery arrays json

我有一个以下格式的 json 数组:

[
  {
     "id":"01",
     "language": "Java",
     "edition": "third",
     "author": "Herbert Schildt"
  },
  {
     "id":"07",
     "language": "C++",
     "edition": "second"
     "author": "E.Balagurusamy"
  }
]

我想要一个仅包含该数组中第一列的结果,格式如下:

[ "01", "07" ]

我尝试了很多代码但都没有成功,有没有办法使用 jquery 来实现这一点。

最佳答案

您可以使用 map() 函数执行以下操作。

var input = [
    {
        "id": "01",
        "language": "Java",
        "edition": "third",
        "author": "Herbert Schildt"
    },
    {
        "id": "07",
        "language": "C++",
        "edition": "second",
        "author": "E.Balagurusamy"
    }
];

var result = $(input).map(function() {
    return this.id;
}).get()

console.log(result);

关于jquery - 如何使用jquery从json数组中获取第一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34994618/

相关文章:

jquery - 你能解释一下 $.fancybox.open( [group], [options] ) 参数以及我是否可以添加 youtube 链接作为 href 吗?

javascript - jQuery .wrapAll() 重置嵌套元素的滚动

javascript - Jquery NivoSlider slider 不工作 $ ('#slider' ).nivoSlider();不是函数

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

javascript - 将 JavaScript 对象/映射转换为嵌套数组?

json - 从angularjs中的本地JSON文件中获取数据

json - 在 Swift 中创建具有多个自定义单元格的 UITableView

javascript - 更新滚动和其他滚动事件上的哈希值 - 性能问题

c - 加载按分隔符分隔的字符串到数组

javascript - 使用 Charts.js 从分组的 JSON 对象绘制多线图