質問

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"
    }
  ]
}

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; });

names contains ["Abe", "Bart", "Cletus", "Duffman"].

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top