0

Although the topic of converting JS objects to JS arrays as long been dealt with here, my question is about algorithm optimization.

Let my object be

   myObject = {
        "key q": "value q",
        "key p": "value p",
        "key g": "value g" };

to be converted into an JS array, including both keys and values. So far, I have devised this:

var myArr = [];
var aString = JSON.stringify(myObject);
aString = aString.substring (1, (aString.length-1)); // let's get rid of the braces
myArr = aString.replace (/\:/gi, ',');

It all works smoothly, but is there some faster way than undergoing these (allegedly) slow string methods?

Brice Coustillas
  • 2,363
  • 2
  • 13
  • 18
  • how should look the array after? – Nina Scholz Apr 03 '16 at 07:43
  • [There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – Andreas Apr 03 '16 at 08:06
  • Possible duplicate of [a better way to convert JS object to array](http://stackoverflow.com/questions/6857468/a-better-way-to-convert-js-object-to-array) – Andreas Apr 03 '16 at 08:07
  • @Andreas — You are quite right: it is a Javascript object. I my case, I store it in a .json file. Be that as it may, this subtle distinction of yours has no bearing on the cas. – Brice Coustillas Apr 03 '16 at 09:51
  • @Nina Schloz — "element1", "element2", "element3", … "element n", i.e. values in straight quotation marks (" or '), separated by commas. Is there anything odd about it? – Brice Coustillas Apr 03 '16 at 09:53
  • No, it doesn't "work smoothly". First, you end up with a string, not an array. Second, even colons inside string values will end up getting changed into commas. –  Apr 03 '16 at 13:19
  • Do any of these answers work for you? –  Apr 05 '16 at 17:18

2 Answers2

1

So you're looking for a way to clump keys and values together in an array?

myObject = {
        "key q": "value q, comma, more",
        "key p": "value p",
        "key g": "value g" };


myArr = [];
Object.keys(myObject).forEach(function(k) {
    myArr.push(k, myObject[k])
});

document.write('<pre>'+JSON.stringify(myArr,0,3));

And please never ever attempt to modify a JSON structure with string functions. That's not "slow", it's just plain wrong!

georg
  • 211,518
  • 52
  • 313
  • 390
  • That's not JSON: [There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – Andreas Apr 03 '16 at 08:08
  • @georg — Why 'just plain wrong'? Take a look at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify for this one example provides: var foo = {foundation: "Mozilla", model: "box", week: 45, transport: "car", month: 7}; var jsonString = JSON.stringify(foo, replacer); – Brice Coustillas Apr 03 '16 at 10:00
  • @BriceCoustillas: `JSON.stringify(...replacer)` is fine, `someJsonString.replace(...)` is wrong - just because `String.replace` has no clue about json syntax. – georg Apr 03 '16 at 10:15
  • @georg — You algorithm works but I find it restrictive for quotation marks are stripped: if values contain strings that include commas (my case here) it will result in jumble. Anything more generic? – Brice Coustillas Apr 03 '16 at 10:27
  • @georg — as to someJsonString.replace (…) I got you point. In my own algorithm the replace method comes after extraction with stringify plus stripping with substring. Then I take if fort granted that it is applied to a regular string. – Brice Coustillas Apr 03 '16 at 10:31
  • @BriceCoustillas: "it will result in jumble" - no it won't - see update. – georg Apr 03 '16 at 10:33
  • @BriceCoustillas What do you mean by "quotation marks are stripped"? Quotation marks are merely syntactic devices used to indicate string literals. Please give an example of input that "results in jumble". –  Apr 03 '16 at 13:09
0

If you are a fan of reduce:

function clump(o) {
  return Object.keys(o).reduce((result, k) => result.concat(k, o[k]), []);
}