简体   繁体   English

angularjs扰乱json结构

[英]angularjs disturbs json structure

I'm getting a json from server side in following order: 我按照以下顺序从服务器端获取json:

 [{ "outlet_id": 83 "outlet_name": "My Outlet" "address": "My Outlet" "shop_number": "123" "street": "123" "building_no": "52" "key_location": "Location 123" "mohallah": "Mohalla 123" "landline": "1235869" "owner_name": "Owner" "Manufecture": "A" "BrandName": "B" "Variant": "C" "BRANDDiscription": "D" "SIZE": "E" "Variant/Promotions": null "Segment": null }] 

but when I display it, it disturbs order, I'm using ng-repeat like: 但是当我显示它时,它扰乱了秩序,我正在使用ng-repeat,如:

 <td ng-repeat="(key, value) in vm.outletFieldAttrsList[0]">{{value}}</td> 

order of attributes is not same as order in JSON returned by server, anyone there who cana help? 属性的顺序与服务器返回的JSON中的顺序不同,有谁可以帮助?

Order of object attributes in JavaScript is not guaranteed. JavaScript中的对象属性顺序无法保证。 You need to use an Array or a Map for that. 您需要使用Array或Map。

Relevant: 相关:

AngularJS sorting by property AngularJS按属性排序

Javascript "objects" does not have such thing as an "order" - if you have say Javascript“对象”没有“订单”这样的东西 - 如果你说的话

{ "e": 0, "b": 123, "c": 345"... {“e”:0,“b”:123,“c”:345“......

it won't be enumerated then as e, b,c as it was set in literal - result of enumeration will be simply b, c, e ... (by alphabet). 它不会被枚举为e,b,c,因为它是在文字中设置的 - 枚举的结果将简单地为b,c,e ...(按字母表)。

For proper enumeration you have to store the order in some other entity (like array ["e", "b", "c"]) 对于正确的枚举,您必须将订单存储在其他实体中(如数组[“e”,“b”,“c”])

I think you try use arr.sort, but it is not guaranteed. 我想你尝试使用arr.sort,但不保证。

The sort() method sorts the elements of an array in place and returns the array. sort()方法对数组中的元素进行排序并返回数组。 The sort is not necessarily stable. 排序不一定稳定。 The default sort order is according to string Unicode code points. 默认排序顺序是根据字符串Unicode代码点。

If compareFunction is not supplied, elements are sorted by converting them to strings and comparing strings in Unicode code point order. 如果未提供compareFunction,则通过将元素转换为字符串并按Unicode代码点顺序比较字符串来对元素进行排序。 For example, "Cherry" comes before "banana". 例如,“Cherry”出现在“banana”之前。 In a numeric sort, 9 comes before 80, but because numbers are converted to strings, "80" comes before "9" in Unicode order. 在数字排序中,9出现在80之前,但由于数字被转换为字符串,因此“80”以Unicode顺序出现在“9”之前。

Array.prototype.sort() Array.prototype.sort()

I believe the best solution, It would be the response comes in the order you want. 我相信最好的解决方案,就是响应按照你想要的顺序排列。

Example: 例:

[0: { ITEM }, 1 : {ITEM 2}]

Or create a function comparate : 或者创建一个兼容的功能:

items.sort(function (a, b) {
  return a.localeCompare(b);
});

More details .. , see the reference 更多细节.. ,参见参考资料

I found solution to this problem: 我找到了解决这个问题的方法:

 <td ng-repeat="key in objectKeys(outletFieldAttrsList[0])"> </td> 

and controller side: 和控制器方面:

 $scope.objectKeys = function (obj) { return Object.keys(obj); } 

Object.keys returns keys of that object in same sequence as they exist. Object.keys以与它们相同的顺序返回该对象的键。

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

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