在数组中按数组排序对象?

时间:2015-06-12 11:13:19

标签: javascript jquery arrays sorting local-storage

我有一个本地存储,如下所示:

Key: Savedme        
Value:
{
 "Bob":["1","1"],
 "John":["2","1"],
 "Mom":["3","1"],
 "Dad":["1","2"],
 "Tom":["3","2"],
 "Skipper42":["2","3"],
 "Hated_41":["3","3"],
 "Greeneggs":["2","2"],
 "William":["1","3"]
}

我需要以某种方式对它进行排序,看起来像这样

{
 "Bob":["1","1"],
 "Dad":["1","2"],
 "William":["1","3"]
 "John":["2","1"],
 "Greeneggs":["2","2"],
 "Skipper42":["2","3"],
 "Mom":["3","1"],
 "Tom":["3","2"],
 "Hated_41":["3","3"]
}

我已经尝试将其存储在如下的矩阵中:

var $runthrough = [[]];
$runthrough[$x,$y] = $values;

其中x是第一组数字,y是下一个,然后值是Bob,Dad等......从那里我可以为矩阵的两个部分做一个foreach并且它会完成,但是当我在运行一组对象后使用此方法,第二组给出“未定义”,即使我已设置了一些要检查的触发器,并且它实际上没有未定义。

var loadarray = JSON.parse(localStorage.getItem( 'savedme' ));
$.each(loadarray, function(k, v) {
     if(typeof k === 'undefined' || !k){
        console.error("undefined found at k!");
    };
     if(typeof v[0] === 'undefined' || !v[0]){
        console.error("undefined found at x!");
    };
     if(typeof v[1] === 'undefined' || !v[1]){
         console.error("undefined found at y!");
    };
});

所以我已经意识到,我可能在做错了数组,所以我认为排序数组会更快,然后使用相同的函数。它是这样排序的,因为它基本上会输出到矩阵表,我试着这样命令:

  {
    "1":["1","Bob"],
    "2":["1","John"],
  } 

但是......索引值1只会被最后一个值

覆盖

4 个答案:

答案 0 :(得分:2)

对象属性在JavaScript中没有保证顺序,您需要使用Array

来自ECMAScript Third Edition (pdf)的对象的定义:

  

4.3.3对象
  一个对象是一个成员   类型对象。 这是一个无序的属性集合   包含原始值,对象或   功能。存储在a中的函数   对象的属性称为a   方法

尝试使用这样的数据结构:

[
 { name: "Bob", value: ["1","1"] },
 { name: "Dad", value: ["1","2"] },
 { name: "William", value: ["1","3"] },
 { name: "John", value: ["2","1"] },
 { name: "Greeneggs", value: ["2","2"] },
 { name: "Skipper42", value: ["2","3"] },
 { name: "Mom", value: ["3","1"] },
 { name: "Tom", value: ["3","2"] },
 { name: "Hated_41", value: ["3","3"] }
]

您可以像这样生成此结构:

var loadarray = JSON.parse(localStorage.getItem( 'savedme' ));
var sorted = [];
for (var prop in loadarray) {
    if (loadarray.hasOwnProperty(prop)) {
        sorted.push({name:prop, value:loadarray[prop]});
    }
}
sorted.sort(function(a, b) {
    var v0 = a.value[0] - b.value[0];
    return v0 == 0 ? a.value[0] - a.value[0] : v0;
});

答案 1 :(得分:2)



ActiveRecord::Relation

var a = [
  { "Bob": ["1", "1"] },
  { "John": ["2", "1"] },
  { "Mom": ["3", "1"] },
  { "Dad": ["1", "2"] },
  { "Tom": ["3", "2"] },
  { "Skipper42": ["2", "3"] },
  { "Hated_41": ["3", "3"] },
  { "Greeneggs": ["2", "2"] },
  { "William": ["1", "3"] }
];

a.sort(function (a, b) {
  var aa = a[Object.keys(a)],
      bb = b[Object.keys(b)];
  if (aa[0] === bb[0]) {
    return aa[1] - bb[1];
  } else {
    return aa[0] - bb[0];
  }
});
document.querySelector("#demo").innerHTML = JSON.stringify(a, null, 4);




答案 2 :(得分:0)

无法在对象中对进行排序。

但是,可以使用Object.keys(object).sort()按顺序处理

在这里,我将对象输出到一个数组 - 按键排序'值 - 然后显示该数组:



var obj= {
 "Bob":["1","1"],
 "John":["2","1"],
 "Mom":["3","1"],
 "Dad":["1","2"],
 "Tom":["3","2"],
 "Skipper42":["2","3"],
 "Hated_41":["3","3"],
 "Greeneggs":["2","2"],
 "William":["1","3"]
}

var arr= Object.keys(obj)
          .sort(function(a, b) {
            if(obj[a][0]===obj[b][0]) {
              return obj[a][1] - obj[b][1];
            }
            else {
              return obj[a][0] - obj[b][0];
            }
          })
          .map(function(key) {
            var o= {};
            o[key]= obj[key];
            return o;
          });

document.body.innerHTML= JSON.stringify(arr);




答案 3 :(得分:0)

很晚了,但是如果您有一个数组,可以尝试一下:

var data=[{Value:
{
 "Bob":["1","1"],
 "John":["2","1"],
 "Mom":["3","1"],
 "Dad":["1","2"],
 "Tom":["3","2"],
 "Skipper42":["2","3"],
 "Hated_41":["3","3"],
 "Greeneggs":["2","2"],
 "William":["1","3"]
}}];

data.forEach((elem)=>{
    for(const property in elem.Value){
        var k = elem.Value[property];
        k.sort((a,b)=>a-b);
    }
});

console.log(data);