Javascript - 从对象开始维护关键顺序 - >排列 [英] Javascript - maintain key order when going from object -> array

查看:78
本文介绍了Javascript - 从对象开始维护关键顺序 - >排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道JS对象不保证密钥顺序,但是,我的数据结构来自我无法控制的后端。从以下地方我可以做些什么来保存关键顺序:

I know key order isn't guaranteed in JS objects, however, my data structure comes from a backend for which I have no control over. Is there anything I can do to preserve the key order when going from:

var obj = {
   foo: 'bar',
   bar: 'foo' 
};

to:

Object.keys(obj); // hoping for ['foo', 'bar']

我保持最重要的不幸的是关键顺序......

It's of the utmost importance that I keep the key order unfortunately...

推荐答案

没有。正如您所写:


我知道JS对象中无法保证关键顺序

I know key order isn't guaranteed in JS objects

如果您需要订单,则需要使用数组。如果您有一个对象,则该属性没有已定义的顺序。

If you want the order you need to use an array. If you have an object, then there is no defined order to the properties.

这篇关于Javascript - 从对象开始维护关键顺序 - >排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆