按数组中的数组对对象进行排序?

Posted

技术标签:

【中文标题】按数组中的数组对对象进行排序?【英文标题】:Sort object by array within array? 【发布时间】:2015-08-28 09:08:09 【问题描述】:

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

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 会被最后一个值覆盖

【问题讨论】:

您不能在 javascript 中对对象进行排序 【参考方案1】:

您可以使用相同的索引链接值的增量。

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);
<div id="demo"></div>

2021 年更新

const
    array = [ 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"] ];

array.sort((a, b) => 
    const
        aa = Object.values(a)[0],
        bb = Object.values(b)[0];
 
    return aa[0] - bb[0] || aa[1] - bb[1];
);

console.log(array);
.as-console-wrapper  max-height: 100% !important; top: 0; 

【讨论】:

【参考方案2】:

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

从ECMAScript Third Edition (pdf)定义一个对象:

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

试试这样的数据结构:

[
  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;
);

【讨论】:

【参考方案3】:

键不能在一个对象内进行排序

但是,它们可以使用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);

【讨论】:

【参考方案4】:

很晚,但你可以试试这个,假设你有一个数组:

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);

【讨论】:

以上是关于按数组中的数组对对象进行排序?的主要内容,如果未能解决你的问题,请参考以下文章

如何按对象中的值对数组进行排序

JS按数组中的“x”对对象进行排序[重复]

在PHP中的函数内按升序对对象数组进行排序[重复]

除数字外,如何按字母顺序对对象数组进行排序?

如何在 React.js 中按字母顺序对数组中的对象进行排序

按对象属性对 javascript 对象数组进行排序