将对象值提取到数组中 [英] Extract Object Values into an Array

查看:109
本文介绍了将对象值提取到数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am needing to pull all of the names out of the object below and store into an array. the reason I am needing this is to be able to use the JQuery tag-it plugin for auto complete. So if you know of a better way I can do this other than creating a new array I am all ears.

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

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

Expected Output = ["scott","bob","betty","ted"]

推荐答案

var arr = [{name:'Abe'},{name:'Bart'},{name:'Cletus'},{name:'Duffman'}];
var names = arr.map(function(a) { return a.name; });

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

这篇关于将对象值提取到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆