通过匹配其他数组来更改对象属性的位置 [英] Change position of object properties by matching other array

查看:92
本文介绍了通过匹配其他数组来更改对象属性的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象

var $data = {
  D_1_AA_Changes.xml: "This is a string", 
  D_2_Compare_AA_Changes.xml: "This is a string"
}

我需要将该属性与我拥有的另一个数组进行匹配:

I need to match the property with another array I have like this:

var list_match = ["D_2_Compare_AA_Changes","D_1_AA_Changes"]; 

我需要将list_match$data属性进行匹配,并将$data对象布置为list_match数组值.

I need to match the list_match with $data property and arrange the $data object as list_match array values.

所以匹配后,我必须像这样安排$data:

so after matching I have to arrange $data like this:

$data = {
  D_2_Compare_AA_Changes.xml: "This is a string",
  D_1_AA_Changes.xml: "This is a string"
}

所以我需要像上面这样的最终结果

So I need the final result like the above

推荐答案

可以在完全兼容的ES2015 + JavaScript引擎中执行此操作,但是通常,最好将对象属性视为无序的,因为许多对象属性即使在ES2015 +中,操作(如for-in)也不能保证遵循新操作(如Object.getOwnPropertyNamesJSON.stringify)使用的顺序.序列化和反序列化时(例如,往返于JSON),也不一定必须保持顺序.它可能是,但不能保证是.*依靠无保证的行为会导致细微的,浪费时间的错误.

It is possible to do this in a fully-compliant ES2015+ JavaScript engine, but in general, it's best to treat object properties as being unordered, since many object property operations (like for-in), even in ES2015+, are not guaranteed to follow the order that is used by the new ones (like Object.getOwnPropertyNames, JSON.stringify). Order is also not necessarily maintained when you serialize and deserialize (for instance, going to and from JSON). It may be, but it isn't guaranteed to be.* Relying on unguaranteed behavior leads to subtle, time-wasting bugs.

但是,如果要在ES2015 +引擎上执行此操作,请创建一个新对象,并按照希望它们出现的顺序添加这些属性:

But if you want to do it on an ES2015+ engine, create a new object and add those properties in the order you want them to be in:

const $data = {
  "D_1_AA_Changes.xml": "This is a string",
  "D_2_Compare_AA_Changes.xml": "This is a string"
};

var list_match = ["D_2_Compare_AA_Changes","D_1_AA_Changes"];

console.log("before", JSON.stringify($data));

const $newData = {};
for (const entry of list_match) {
  const name = entry + ".xml";
  $newData[name] = $data[name];
}

console.log("after", JSON.stringify($newData));

请注意,在这种情况下它可以工作,因为属性名称看起来不像数组索引,而这些是自有"属性.看起来像数组索引的属性名称没有像其他属性名称那样按创建顺序列出.

Note that it works in this case because the property names don't look like array indexes, and these are "own" properties. Property names that look like array indexes aren't listed in creation order like other property names are.

再次如此:强烈建议不要尝试使用对象属性顺序.

So again: Strongly recommend not trying to use object property order.

更多阅读内容:

  • The spec on the order of "own" properties in an object
  • The spec on the order of "own" and inherited properties in an object
  • Does ES6 introduce a well-defined order of enumeration for object properties? the answers to which outline the operations that do and don't follow property order

* JSON.stringify确保遵循该顺序,但不一定是该JSON的消费者.

* JSON.stringify is guaranteed to follow the order, but the consumers of that JSON are not, necessarily.

这篇关于通过匹配其他数组来更改对象属性的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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