axios:停止基于键的对象自动分类 [英] axios: stop automatic sorting of objects based on keys

查看:182
本文介绍了axios:停止基于键的对象自动分类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用axios获取api响应.

I am gettting an api response using axios.

我正在发送带有w.r.t名称http://localhost:8000/api/ingredients/ordering=name

I am sending the api response sorted w.r.t name http://localhost:8000/api/ingredients/ordering=name

从我的服务器发送的实际对象如下.

The actual object sent is as below from my server.

{
    2:{"id":2,"name":"anil"},
    1:{"id":1,"name":"sant"},
    3:{"id":3,"name":"vino"}
}

或将密钥作为字符串而不是整数传递

or pass the key as string instead of integer

{
    "2":{"id":2,"name":"anil"},
    "1":{"id":1,"name":"sant"},
    "3":{"id":3,"name":"vino"}
}

或键为字母数字

{
    "a2":{"id":2,"name":"anil"},
    "a1":{"id":1,"name":"sant"},
    "a3":{"id":3,"name":"vino"}
}

axios所做的是使用键对对象进行排序,并且名称未按排序顺序

what axios is doing is sorting the objects w.r.t keys and the names are not in sorted order

{
    1:{"id":1,"name":"sant"},
    2:{"id":2,"name":"anil"},
    3:{"id":3,"name":"vino"}
}

或者如果键是字母数字

{
    a1:{"id":1,"name":"sant"},
    a2:{"id":2,"name":"anil"},
    a3:{"id":3,"name":"vino"}
}

如何停止此操作

我不需要数组格式的列表,因为稍后我必须根据ID更新各个对象.这种表示很容易.我正在使用reactjs状态.

I dont want in array format the list because i have to update the individual objects later based on the id. This representation is easy. I am using reactjs state.

推荐答案

此处有答案大多数浏览器以插入时返回的顺序返回属性,但显然不能保证其行为,因此不应依赖于此.

most browsers returns properties in the same order as they were inserted, but it was explicitly not guaranteed behaviour so shouldn't have been relied upon.

ECMAScript规范用于说:

枚举属性的机制和顺序...不是 指定.

The mechanics and order of enumerating the properties ... is not specified.

但是在ES2015及更高版本中,非整数密钥将按插入顺序返回.

However in ES2015 and later non-integer keys will be returned in insertion order.

所以不要依赖对象键的顺序.如果您不担心ES5,可以尝试使用非整数键-例如字符串,但请注意,像'1'这样的字符串不适合

So don't rely on order of an object keys. If you don't worry about ES5 you can try to use non-integer keys - strings for instance but note that strings like '1' are not suitable http://code.google.com/p/v8/issues/detail?id=164.

您还可以在答案页面上找到

You can find on the page of the answer also:

如果您依赖插入顺序,那么您将不在ECMAScript规范之内,但是只要您的键不解析为整数,就可以在常见浏览器行为的事实上的标准之内.

If you rely on insertion order, you are outside the ECMAScript spec, but within the de-facto standard of common browser behavior as long as your keys don't parse as integers.

更新

最简单的答案是axios不会对对象的键进行排序

Short answer is axios doesn't sort keys of an object

https://jsfiddle.net/zabq841t/

axios.get('/echo/js/?js={a2: 1, a1: 0}')
    .then((res) => {
        //res.data is {a2: 1, a1: 0}
    })

由于浏览器或服务器旧,您可以获得排序的版本.看一下小提琴,如果它对您有用,那么您可能在服务器端出现了问题".您需要提供有关服务器应用程序的更多详细信息.

You can get sorted version because of old browser or your server. Take a look fiddle if it works for you then probably you have 'issue' on server side. You need to provide more details about you server app.

这篇关于axios:停止基于键的对象自动分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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