排序复杂对象数组jQuery

时间:2018-10-19 15:11:48

标签: javascript jquery sorting

我有一个ID为键和array [4]值的对象

myArray = { 
            5bb4c21d2d49b2223066cf62:[1539890687, "GEL-Kayano 25 OBI s", 0, "Black"],
            5bb4c21e2d49b22230670aec:[1539877225, "Nike SB Check Solar Leather Skate Shoes", 0,"White"], 
            5bb4c21e2d49b2223067455e:[1539890979, "GEL-Quantum 360 4", 0, "Grey"],
            5bb4c21e2d49b22230672934:[1539890693, "GEL-Contend 4 Wide Width s", 0, "Blue"],
            5bb4c21f2d49b222306766b9:[1539890671, "GEL-DS Trainer 23 Casual Shoes", 0, "White"],
            5bb7aadfda02b1355e3c0cdb:[1539890596, "Womens ASICS GT-2000 V6", 0, "Black"]
}

如何根据时间戳对对象进行排序,即数组[4]中的第一个值?

1 个答案:

答案 0 :(得分:0)

我对您的代表作了一些更改。检查以下方法是否可以解决您的问题。

var myArray = [
            {"5bb4c21d2d49b2223066cf62":[1539890687, "GEL-Kayano 25 OBI s", 0, "Black"]},
            {"5bb4c21e2d49b22230670aec":[1539877225, "Nike SB Check Solar Leather Skate Shoes", 0,"White"]}, 
            {"5bb4c21e2d49b2223067455e":[1539890979, "GEL-Quantum 360 4", 0, "Grey"]},
            {"5bb4c21e2d49b22230672934":[1539890693, "GEL-Contend 4 Wide Width s", 0, "Blue"]},
            {"5bb4c21f2d49b222306766b9":[1539890671, "GEL-DS Trainer 23 Casual Shoes", 0, "White"]},
            {"5bb7aadfda02b1355e3c0cdb":[1539890596, "Womens ASICS GT-2000 V6", 0, "Black"]}
];
myArray.sort((a,b) =>{
  return Object.values(a)[0][0] - Object.values(b)[0][0];
});
console.log(myArray);