max
max

Reputation: 108

How to use received data by ajax?

This code works well and I am receiving data from server. Data consists of [{id: someId, name: someName}, {..}....{...}]

SJA.ajax(dataToSend, function (respond) {
  if (respond) {
     for (var i in respond) {
        console.log(respond[i].id);
        console.log(respond[i].name);
      }
   }
});

How can I create an array from only respond[i].name and use this array in such a way:

var data = ["name", "name",  "name"];

Upvotes: 0

Views: 54

Answers (2)

Gene R
Gene R

Reputation: 3744

var data = $.map(respond, function (v) { return v.name; });

Upvotes: 0

AKX
AKX

Reputation: 168843

Simply

var names = respond.map(function(r) { return r.name; });

will do what you want.

Upvotes: 2

Related Questions