0

I have an Object

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"]; 

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

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

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
Hassaan
  • 319
  • 2
  • 26
  • why not take the array for accessing order of the array? – Nina Scholz Oct 28 '17 at 10:34
  • finally i need the object after matching the order... can you provide me any example – Hassaan Oct 28 '17 at 10:35
  • There is no 'order' notion in a javascript object. You don't know how the properties are internally managed and cannot rely on the 'position' you gave them when inserting in the object. – ValLeNain Oct 28 '17 at 10:36
  • This data object is a dynamic object created from some xml file dear, so i dont have access to that file...I am just getting this data object and needs to arrange in order i provided above – Hassaan Oct 28 '17 at 10:38

2 Answers2

2

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.

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.

More reading:


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

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

You can use the order in the array to access the object properties like so

list_match.forEach(function(key) {
  var myData = $data[key];
  /* you can check that `myData` is not undefined */
})
ValLeNain
  • 2,232
  • 1
  • 18
  • 37