-2

Hope you have been a good day. I want to ask you a question. Here is it:

So I have an object and I get values from it with Underscore.js (I will give you an example without using underscore.js to getting values).

After I get values, it puts numbers that are bigger than 0. I think it sorts bigger than smaller.

I want values exactly how they are at the object before I take them.

Here is an image for you

enter image description here

Here is the code

tmpReport.forEach(element => {
            const tmpVal = [];
            console.log("element: ", element);
            tmpValList.push(_.values(element));
            console.log('tmpValList: ', tmpValList);
        });

Here is the code without using underscore.js:

tmpReport.forEach(element => {
            const tmpVal = [];
            console.log("element: ", element);
            Object.entries(element).forEach(([key, value]) => {
                tmpVal.push(value);
            });
            tmpValList.push(tmpVal);
            console.log('tmpValList: ', tmpValList);
        });

So any help will be appreciated. Thank you!

eko
  • 39,722
  • 10
  • 72
  • 98
oldner
  • 115
  • 2
  • 12
  • How do you know that's not just the order of the values? Have you tried debugging/logging the `tmpVal.push(value);` iteration to see in what order you get values in your arrays? – VLAZ Jun 01 '21 at 18:04
  • @VLAZ I thought it should be same with the array that is showed on console. Can they be different? – oldner Jun 01 '21 at 18:13
  • 1
    Consoles are free to display objects however they like. There is no standard to how exactly they log objects. The one you've expanded seems like it has its keys ordered in alphabetical order. You can see the unexpanded instead starts with `time`. – VLAZ Jun 01 '21 at 18:16
  • now I got the idea. So if I sort, then it will be like how it looks on console. Thanks. – oldner Jun 01 '21 at 18:19

1 Answers1

2

In JavaScript, the order of object entries is not guaranteed. If you want to put specific entries at specific indices in your array you have to do that manually.

For example:

tmpVal[0] = element['Cell damage by placing'];
...
tmpVal[4] = element['time'];
tmpVal[5] = element['toplan'];

You can only achieve that if you are aware of your object keys. As far as I know there is now generic way to keep the initial order as there is no order to begin with.

The best you can do in that case is use a lexicographic order and sort your keys accordingly.

iodine
  • 41
  • 3
  • 1
    "*In JavaScript, the order of object entries is not guaranteed.*" [Does ES6 introduce a well-defined order of enumeration for object properties?](https://stackoverflow.com/q/30076219) - it is. But still probably shouldn't be relied on. – VLAZ Jun 01 '21 at 18:17
  • Good to know. Thank you. – iodine Jun 01 '21 at 18:27
  • You can do `tmpVal = _.map(['Cell damage by placing', 'time', ...], _.propertyOf(element))` in order to extract the values into an array by the given order of keys. – Julian Jun 06 '21 at 10:36