jQuery each always sort it?

前端 未结 4 506
被撕碎了的回忆
被撕碎了的回忆 2020-12-20 13:03

I have this object in JS:

var list = {134 : \"A\",140 : \"B\",131 : \"C\"}

I run it with:

jQuery.each(list, function(key, v         


        
4条回答
  •  没有蜡笔的小新
    2020-12-20 13:59

    This happens because JavaScript object items do not have order.

    In order to fix it you may use two arrays: one with the keys, and second with values:

    var keys = [134, 140, 131],
        values = ["A", "B", "C"];
    
    $.each(keys, function(i, key) {
        console.log(key, values[i]);
    });
    

提交回复
热议问题