Rorrik
Rorrik

Reputation: 370

Concat doesn't seem to work on a JSON obtained array

I'm having a some trouble with the concat function in javascript. As far as I can tell, I'm using it correctly, but it's not successfully concatenating the array "value" from the JSON onto the array "currentAbilities":

$.each(ABILITIES, function(key,value) {
        if(key==raceName||(key==className&&(!isAcolyte)))
        {
            document.getElementById('abs').innerHTML = value[1];
            currentAbilities.concat(value);
        }
    })

The innerHTML setting line shows that the array "value" does exist. Any idea why I'm failing to concat? Value of course comes from a JSON, could that have anything to do with it? I'm afraid I only recently became familiar with $.each and JSON usage and may be doing it wrong.

Upvotes: 1

Views: 2066

Answers (1)

user1106925
user1106925

Reputation:

Assuming the JSON has been parsed, you need to keep a reference to the new Array created by concat() because the .concat() method does not modify the original...

currentAbilities = currentAbilities.concat(value);

Or just use .push with .apply if you want to modify the currentAbilities array...

currentAbilities.push.apply(currentAbilities, value)

Also, make sure your raceName and className variables are what you expect.

Upvotes: 3

Related Questions