soum
soum

Reputation: 1159

printing object stored in an array using jquery

I have an array storing some data in the following format

var arrayTest = [{
    "firstName": "SOUM!",
     "lastName": "Ghosh"
}, {
    "firstName": "Fred",
    "lastName": "Twilliger"
}, {
    "firstName": "Snoop",
    "lastName": "Krieg"
}

]

I am trying to parse each key items

here is my script

    var html = "";

$.each(arrayTest, function () {
    html + = this.firstName;
});

alert(html);

what am I doing wrong.

Here is my fiddle http://jsfiddle.net/sghoush1/J2ssK/6/

Upvotes: 0

Views: 52

Answers (3)

Ahmed Abumostafa
Ahmed Abumostafa

Reputation: 218

$.each(arrayTest, function (i, row) {
    html += row.firstName;
});

http://jsfiddle.net/J2ssK/8/

Upvotes: 0

Phil
Phil

Reputation: 164776

String concatenation is so last century

var html = arrayTest.map(function(n) {
    return n.firstName;
}).join('');

Upvotes: 0

Brian
Brian

Reputation: 7654

SyntaxError: Unexpected token =

So... you can't have a space between + and =

Upvotes: 2

Related Questions