Вопрос

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