最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Associative array not in order - Stack Overflow

programmeradmin5浏览0评论

Order of associative array is like this

A00 = > value1
A01 = > value2
B01 = > value3
B02 = > value4

But after for loop order of array doesn't working

for (var key in obj3) {
    $("#code_list1").append(obj3[key]);
}

Console output is as in image

Order of associative array is like this

A00 = > value1
A01 = > value2
B01 = > value3
B02 = > value4

But after for loop order of array doesn't working

for (var key in obj3) {
    $("#code_list1").append(obj3[key]);
}

Console output is as in image

Share Improve this question edited Apr 16, 2015 at 13:08 priti narang asked Apr 16, 2015 at 12:53 priti narangpriti narang 2365 silver badges18 bronze badges 4
  • 5 There are no associative arrays in javascript, so lets assume you really have an object, and there is no order in objects, so not seeing an ordered iteration is perfectly normal. – adeneo Commented Apr 16, 2015 at 12:54
  • 1 To add to what @adeneo said, if you instead have an array you can implement your own sort() logic to force the required order. Seeing the actual values you have would help provide a more concrete answer. – Rory McCrossan Commented Apr 16, 2015 at 12:58
  • Please see the attached image having console output. As I know jQuery for loop note maintaining the order of the array stackoverflow./questions/18804592/… – priti narang Commented Apr 16, 2015 at 13:02
  • Maintaining an order and being ordered are two different things. Object properties may be ordered in no particular order... – Robert Rossmann Commented Apr 16, 2015 at 13:15
Add a ment  | 

2 Answers 2

Reset to default 4

Javascript objects like this do not support order, this is expected behavior.

You can use some simple methods to extract the keys list from this and sort that.

Then use the sorted list to do what you need.

var keys = Object.keys(obj3).sort();

for (var i = 0; i < keys.length; i++) {
    console.log(keys[i]);
    $("#code_list1").append(obj3[keys[i]]);
}

http://jsfiddle/rmvoz6av/3/

As Robert suggested I changed this to Object.keys(), although some older IE browsers won't support this.

In associative arrays, the order isn't considered important. This is part of the definition of the structure. Logistically, of course, there has to be some kind of ordering, so that the puter can keep track of what entries are where, but there is no guarantee that the ordering is based on anything in particular.

This makes it dangerous to simply rely on the "natural order" of elements in an associative array. It may work by coincidence, if your runtume happens to put things in the same order you want them. But coincidental functionality has a nasty habit of breaking when you least expect it: runtimes change. To iterate over an associative array, using some kind of ordering based on the keys, it is best to sort the keys explicitly.

In JavaScript, if you're using Objects as associative arrays, then the Object.keys function provides an easy way to get at the keys in a convenient, sortable format. Browsers support this going back as far as IE9, and it's easy to shim if you need support for earlier browsers.

/**
 * Determine the relative positions of two keys.
 *
 * I don't know exactly how you're determining your ordering here,
 * but unless it's basic alphabetical order, you'll need a function
 * like this for sort() to work properly.
 *
 * @param {*} a - One of the keys to pare.
 * @param {*} b - The other key to pare.
 *
 * @return {Number} The relative positions of the two keys.
 *     - If a should e before b, return a number less than 0.
 *     - If b should e before a, return a number greater than 0.
 *     - If their ordering isn't important, return 0.
 */
function pareKeys(a, b) {
    // This implementation just does what JavaScript does normally
    // for sorting, but it should illustrate how to implement your own.
    if (a < b) { return -1; }
    if (a > b) { return 1; }
    return 0;
}

// The code to iterate over the array.
var keys = Object.keys(obj3).sort(pareKeys),
    key,
    i;
for (i = 0; i < keys.length; i += 1) {
    key = keys[i];
    $("#code_list1").append(obj3[key]);
}
发布评论

评论列表(0)

  1. 暂无评论