Skip to content Skip to sidebar Skip to footer

Assigning Unordered Keys To A Js Array

I am getting a random key value pair,Can i assign it to an array? Its problematic here when I assign it like arr[50] = 'abc' it automatically creates the keys upto 50 like arr[0],a

Solution 1:

Use an object {} instead of an array [].

Objects can act as unordered key-value containers, which is what you seem to be needing here.

// somewhere...var newRatingArr = {};

// your code.var emptyRate = true;
if (typeof (feedArr.latestRating) == 'object') {
    jQuery.each(feedArr.latestRating, function (key, val) {
        newRatingArr[key] = val;
        emptyRate = false;
    });
}

Post a Comment for "Assigning Unordered Keys To A Js Array"