【问题标题】:Extract Object Values into an Array将对象值提取到数组中
【发布时间】:2014-03-29 00:32:33
【问题描述】:

我需要从下面的对象中提取所有名称并存储到一个数组中。 我需要这个的原因是能够使用 JQuery tag-it 插件来自动完成。 因此,如果您知道除了创建一个新数组之外还有更好的方法可以做到这一点。

    {
  "user": [
    {
      "title": "boss",
      "name": "scott"
    },
    {
      "title": "janitory",
      "name": "bob"
    },
    {
      "title": "dictation",
      "name": "betty"
    },
    {
      "title": "vp",
      "name": "ted"
    }
  ]
}

预期输出 = ["scott","bob","betty","ted"]

【问题讨论】:

  • 如果您对图书馆开放,underscore 可以使用pluck 方法来做到这一点。
  • 你能显示你想要的输出(用适当的格式)吗?

标签: javascript arrays json tag-it


【解决方案1】:
var arr = [{name:'Abe'},{name:'Bart'},{name:'Cletus'},{name:'Duffman'}];
var names = arr.map(function(a) { return a.name; });

名称包含["Abe", "Bart", "Cletus", "Duffman"]

【讨论】:

  • var names = $.map(quicktestusers.user,function (a) { return a.name; });
  • 你用的是什么浏览器?
  • 谷歌浏览器和 IE 10
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
相关资源
最近更新 更多