-1

Given the following JS Object

var stk={3:{221:1,220:1,224:1,226:1,198:1},24:{221:1,220:1,224:1,226:1,198:1},33:{198:1},9:{223:1,221:1,220:1,224:1,226:1,198:1},156:{220:1,224:1,226:1}};

console.log(stk[24]);

I want this to return in the order it is in. e.g. console.log(stk[24]);

should return: 221,220,224,226,198 instead of 198,220,221,224,226

I want to prevent automatic sort of this Object's numeric property.

Problem: The problem here is that the I have no control over the construction of var stk; because it is built inside a PHP code from a third party plugin.

Is there any way to return the values in the same order as they are in.

itskashi
  • 11
  • 1
  • 2
  • in which form/type do you get `stk`? – Nina Scholz May 24 '18 at 12:46
  • *the same order as they are in* they have a defined order in the source code, but no order in the data structure. Some vendors return them as defined, some don't, and some mix it up depending on some things (as you're seeing). Unfortunately, you can't get around that and you'd be better off asking the provider to use a correct data structure if they should maintain an order. – alex May 24 '18 at 12:49
  • @alex - Browsers have returned properties like the above in numeric order for more than a decade (probably a lot more), and it's been *specified* behavior since June 2015. – T.J. Crowder May 24 '18 at 12:54
  • It should be a characteristic of javascript. The object key is not actually sorted. It is not stored with any order – attempt0 May 24 '18 at 12:57
  • @AnsonYeung - As I said to alex above, that isn't true, and hasn't been probably ever, but particularly not since ES2015. – T.J. Crowder May 24 '18 at 13:02

1 Answers1

4

can I prevent automatic sort of JS Object numeric property?

You can't. But you shouldn't be paying attention to or caring about the order of properties in JavaScript objects anyway. While they have an order (now), it's best to act as though they're unordered. If you want a specific order, use an array (which is also an object, and that's part of why property names that are integer indexes are visited in numeric order).

The problem here is that the I have no control over the construction of var stk; because it is built inside a PHP code from a third party plugin.

If that's the case, you can't do anything about it. If you could get the PHP to output it as a string, e.g.:

var stk = "{3:{221:1,220:1,224:1,226:1,198:1},24:{221:1,220:1,224:1,226:1,198:1},33:{198:1},9:{223:1,221:1,220:1,224:1,226:1,198:1},156:{220:1,224:1,226:1}}";

...then you could parse it yourself and maintain order. But if you can't even do that, there's nothing you can do about it.

You might be able to bend over backward and find it in the text of the JavaScript, if it's an inline script tag rather than a .js file (if it's a .js file, your ability to read it will depend on whether it's from the same origin as your page).

For example:

var text = $("#from-plugin").text();
var match = /var stk=(\{.*\};)/.exec(text);
console.log(match ? match[1] : "Couldn't find it");
<script id="from-plugin">
// other stuff
var stk={3:{221:1,220:1,224:1,226:1,198:1},24:{221:1,220:1,224:1,226:1,198:1},33:{198:1},9:{223:1,221:1,220:1,224:1,226:1,198:1},156:{220:1,224:1,226:1}};
// other stuff
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

...and then, you'd have to parse it into an array of arrays. Probably not particularly difficult, in this case.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks, As I mentioned the object is created by an external script which is not in our control. – itskashi May 24 '18 at 12:47
  • secondly, we need the order to be preserved. :) This is what the issue is. – itskashi May 24 '18 at 12:49
  • 1
    @itskashi - Sorry I missed that part of the question. See the updated answer. – T.J. Crowder May 24 '18 at 12:51
  • 1
    I feel there are implementation details that aren't being stated that would make solving the OPs problem much more trivial. I find it hard to difficult to understand why any third party code would return this type of data structure if a specific order was required. – Adam Jenkins May 24 '18 at 12:57
  • @T.J.Crowder exactly this is what I am also thinking to do. But when I parse this to an array, the data is still returned in numerical order. Is there any way to parse the "stk" into a regular array with order preserved. Yes, this is an inline JS script with PHP but due to business operation (upgrade issues etc.) we can modify the script. Can only play with the returned "stk" object – itskashi May 24 '18 at 15:58
  • @itskashi - By "array of arrays" above, I mean: `[[3, [[221,1],[220,1],[224,1],[226,1],[198,1]]], ...]` which would preserve order. It sounds like you can use the "find the script and grab the text" idea above, which is good news. Now you just have to parse it, which shouldn't be hard at all. – T.J. Crowder May 24 '18 at 16:05