0

I am trying to convert Objects in an Array to one single Object in javascript. Here are objects in an array and logic to convert it to one single object.

var arrObj=[{"Plain bold italic text": "2"},{29: "1"},{24: "2"},{21: "2"},{26: "2"},{27: "2"}];
     var obj={};
     arrObj.forEach((element, index) => {
          obj[Object.keys(element)[0]] = parseInt(Object.values(element).toString());
      });
      console.log(obj);
Somehow it is my requirement to keep string as well as numbers as keys, but I want to maintain the same order for keys. My logic sorts keys which I don't desire.
Expected output:
{"Plain bold italic text": 2, "29": 1,"24": 2,"21": 2,"26": 2,"27": 2}
new user
  • 91
  • 5
  • it does not work for keys like numbers, because this keys are sorted (first). – Nina Scholz May 21 '18 at 18:11
  • @NinaScholz 29 , 24,21,26 , 27 are not sorted. But operations makes it so. I want to have same order of keys as it apears in array . – new user May 21 '18 at 18:12
  • Object order can't be guaranteed in Javascript – Ason May 21 '18 at 18:16
  • Possible duplicate of [Does JavaScript Guarantee Object Property Order?](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order) – Ason May 21 '18 at 18:17
  • Possible duplicate of [object property order between integer indexes, symbols and strings](https://stackoverflow.com/questions/45827457/object-property-order-between-integer-indexes-symbols-and-strings) – ASDFGerte May 21 '18 at 18:18

4 Answers4

0

No, It is not possible to maintain same order with Object You can maintain order only with Array by shifting value with sort and other functions but you can not same with Object.

Nishant Dixit
  • 4,704
  • 3
  • 13
  • 25
0

You can Hack the object key order with Map()

var myObject = new Map();
var arrObj=[{"Plain bold italic text": "2"},{29: "1"},{24: "2"},{21: "2"},{26: "2"},{27: "2"}];
var result = arrObj.reduce(function(accumulator, object){
     
      for(var key in object){
       myObject.set(key, object[key]);
       return myObject
     }
},{})

for (var [key, value] of result) {
  console.log(key + ' = ' + value);
}

READ MORE: http://www.jstips.co/en/javascript/map-to-the-rescue-adding-order-to-object-properties/

Always Sunny
  • 29,081
  • 6
  • 43
  • 74
-1

This isn't doing exactly what you want, but it's the only way I know of to keep everything in the same order - by prepending an underscore, it causes all of the keys to behave like strings and stay in the right order. I can't imagine what your use case is, but this would potentially give you a workaround.

var arrObj=[{"Plain bold italic text": "2"},{29: "1"},{24: "2"},{21: "2"},{26: "2"},{27: "2"}];
     var obj={};
     arrObj.forEach((element, index) => {
          obj["_" + Object.keys(element)[0]] = parseInt(Object.values(element).toString());
      });
      console.log(obj);
jmcgriz
  • 2,701
  • 1
  • 7
  • 10
  • Note that by spec, `Object.keys` does not guarantee order. It's only that most implementations give you the same order as e.g. `Object.getOwnPropertyNames`. – ASDFGerte May 21 '18 at 18:26
  • Oh I know, this is by no means a good idea, just the one way of mimicking what OP is attempting to do – jmcgriz May 21 '18 at 18:28
-2

the problem that you have is that the properties order in objects is not guaranteed in JavaScript; you need to use an Array in order to preserve it (as you have in your objArray.

Definition of an Object from ECMAScript:

Object

An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method.

another solution would be a map instead of a plain object as this article says: enter link description here

here you have a working example with map:

var arrObj = [{
  "Plain bold italic text": "2"
}, {
  29: "1"
}, {
  24: "2"
}, {
  21: "2"
}, {
  26: "2"
}, {
  27: "2"
}];
var obj = new Map();

arrObj.forEach((element, index) => {
  obj.set(Object.keys(element)[0], parseInt(Object.values(element).toString()));
});
for (var [key, value] of obj) {
  console.log(key, value);
}
Prince Hernandez
  • 2,942
  • 1
  • 6
  • 17
  • 1
    Copy paste answer is not a good thing https://stackoverflow.com/a/5525820/6236938. – Nishant Dixit May 21 '18 at 18:17
  • @NishantDixit you are incorrect, I added a proper solution and a link to where I got the code to make the users code work. – Prince Hernandez May 21 '18 at 18:26
  • The issue with your copy answer is that the spec where that definition comes from is very very old. You probably copied that from [the question that pops up first on google](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order) that is from 2011. – ASDFGerte May 21 '18 at 18:28
  • @ASDFGerte but it behaves the same at the moment, it didn't change, so actually it doesnt matter if the definition is new or old as the universe. I added the definition and a solution using another structure (`Map`). dont know what is the problem with that :( – Prince Hernandez May 21 '18 at 18:34
  • It did change considerably in ES6. You can even see in the comments of the answer you copied from that people are stating things like "This answer is false in ES2015". Here is the afaik currently apropriate [question/answer pair](https://stackoverflow.com/questions/30076219/does-es6-introduce-a-well-defined-order-of-enumeration-for-object-properties). – ASDFGerte May 21 '18 at 18:34
  • As a fun fact - if you post anything about object property order on stackoverflow you are almost guaranteed to get half a dozen wrong or outdated answers/comments. The topic is just very confusing and also changed over time. Also using object properties' order is not what one should do, therefore treating them as unordered is usually the best practice anyways. – ASDFGerte May 21 '18 at 18:42