简体   繁体   English

我们如何自定义排序 JavaScript 对象键?

[英]How can we custom sort JavaScript object keys?

I am trying to custom sort a JavaScript object but not able to get how we can do this in a right way.我正在尝试对 JavaScript 对象进行自定义排序,但无法了解如何以正确的方式执行此操作。 Say I am having an object like说我有一个像

var test = {
  'yellow': [],
  'green': [],
  'red': [],
  'blue': []
}

And I have an array with values of : var arr = ['red', 'green', 'blue', 'yellow'];我有一个数组,其值为: var arr = ['red', 'green', 'blue', 'yellow'];

So after sorting, I want to output in the order which is specified in array like所以排序后,我想按照数组中指定的顺序输出

var test = {
  'red': [],
  'green': [],
  'blue': [],
  'yellow': []
}

But for some reason, I am not able to achieve that.但出于某种原因,我无法实现这一目标。 What am trying to do here is loop and sort but it sorts in AZ pattern and not in the order I've specified the array in. Also, you might ask that am not even using my array in the sort function but am not getting an exact way to achieve this.我在这里尝试做的是循环和排序,但它以 AZ 模式排序,而不是按照我指定数组的顺序。此外,您可能会问,我什至没有在排序函数中使用我的数组,但没有得到实现这一目标的确切方法。

var test = {
  'yellow': [],
  'green': [],
  'red': [],
  'blue': []
}

var keys = Object.keys(test);

console.log(keys.sort());  //["blue", "green", "red", "yellow"] which is not ordered nor it's an object

Any directions will he helpful.任何方向都会有帮助。


Note: I am having this specific requirement because am using HandleBars with default {{#each}} block, but I want to loop the inner objects in that order.注意:我有这个特定要求,因为我使用带有默认{{#each}}块的 HandleBars,但我想按该顺序循环内部对象。 I can loop the objects in Handlebars and pass them to the template but than am not using the power of template engine.我可以在 Handlebars 中循环对象并将它们传递给模板,但我没有使用模板引擎的强大功能。 I want Handlebars to loop them or am I missing something?我想要 Handlebars 来循环它们还是我遗漏了什么? Is it fine if I loop the objects and pass the entire markup to handlebars template.如果我循环对象并将整个标记传递给把手模板,是否可以。

JavaScript objects are hashes and therefore inherently un-ordered. JavaScript 对象是散列,因此本质上是无序的。 You cannot make an object with properties in any given order.您不能以任何给定的顺序创建具有属性的对象。 If you are receiving an ordering when you enumerate the keys, it is purely coincidental, or better said, an implementation detail of the JavaScript engine.如果您在枚举键时收到一个排序,这纯粹是巧合,或者更确切地说,是 JavaScript 引擎的一个实现细节。

You'd have to use an array of objects to achieve something like what you want to do.您必须使用一组对象来实现您想要做的事情。

In other words, it is not possible with the data structure you have chosen.换句话说,您选择的数据结构是不可能的。

You could use Map()你可以使用Map()

A Map object iterates its elements in insertion order Map 对象按插入顺序迭代其元素

 var arr = ['red', 'green', 'blue', 'yellow']; var map = new Map(); arr.forEach(function(val, index) { var obj = {}; obj[val] = []; map.set(index, obj) }); console.log(map)

May be you could change this using JSON.stringify()也许你可以使用 JSON.stringify() 改变它

do like喜欢

var json = {     "name": "David",     "age" : 78,     "NoOfVisits" : 4   };
console.log(json);
//outputs - Object {name: "David", age: 78, NoOfVisits: 4}
//change order to NoOfVisits,age,name

var k = JSON.parse(JSON.stringify( json, ["NoOfVisits","age","name"] , 4));
console.log(k);
//outputs - Object {NoOfVisits: 4, age: 78, name: "David"} 

put the key order you want in an array and supply to the function.把你想要的键顺序放在一个数组中并提供给函数。 then parse the result back to json.然后将结果解析回 json。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM