2

One of my node js libraries is returning some data I need in the wrong format like this:

{"a":["1","2"],"b":["3","4"],"c":["5","6"]}

(note that the values don't matter) but I need to loop this array in a way, that I find my B for my A that has a certain value (in this case e.g. '2', I would need '4') and all other parts of my program are so far using arrays like this:

[{"a":"1", "b":"3", "c":"5"}, {"a":"2", "b":"4", "c":"6"}]

and it would be my preferred approach.

Also note that the amount of data in a is always the same as b and c, but itself is variable.

So what would be the "best" way to accomplish this in ES6/JS (before I start messing with for-loops)?

ThexBasic
  • 715
  • 1
  • 6
  • 24
  • "*would be the "best" way*"... if this question is holding you up in your progression, then I would strongly encourage you to read this: http://www.joelonsoftware.com/articles/fog0000000018.html – Travis J Nov 23 '16 at 00:24
  • 2
    The example of using the array shown here has an error, notably "*Uncaught SyntaxError: Unexpected token ,*". Did you intend that to be an array? I feel it is a critical piece of your question. – Travis J Nov 23 '16 at 00:26
  • 1
    Actually for-loops would be a quite good idea. – Bergi Nov 23 '16 at 00:31
  • If you wanted, you could get [quite](http://stackoverflow.com/a/23849462/1048572) [fancy](http://stackoverflow.com/a/30315773/1048572)… – Bergi Nov 23 '16 at 00:38
  • @TravisJ yes, it is intended as array. I was just typing it as an example. Also, I know the talk about why use the "best"? First: this particular code would be called quite often, so optimizing it would be good. Second: if we just thought "oh thats good enough" - we wouldn't be where we are now in terms of tech or anything :) – ThexBasic Nov 23 '16 at 00:43
  • `I was just typing it as an example` - your example is not valid javascript. How can anyone help change your "format" if your required "format" is invalid? – Jaromanda X Nov 23 '16 at 00:44
  • yes you are right, I wasn't paying attention to the difference in brackets and have corrected that right now. I am assuming that my description in text was suffcient to make up for that mistake in terms of understanding though. – ThexBasic Nov 23 '16 at 01:14
  • *messing with for-loops* For loops, whether explicit or implicit (as in `map` etc.) are the foundation of programming iterative structures--why would you refer to them as "messing"? –  Nov 23 '16 at 03:57

1 Answers1

2

If you are looking to transform an object like

{"a":["1","2"],"b":["3","4"],"c":["5","6"]}

Into a array like

[{"a":"1","b":"3","c":"5"},{"a":"2","b":"4","c":"6"}]

Something like this is the simplest way I can think of

function formatData (data) {
  return Object.keys(data).reduce((arr, key) => {
    data[key].forEach((value, i) => {
     const iObj = arr[i] || (arr[i] = {});
     iObj[key] = value;
    });
    return arr;
  }, []);
}
  • You're question had `{["a":"1", "b":"3", "c":"5"], ["a":"2", "b":"4", "c":"6"]}` which I'm assuming you meant to be `[{"a":"1","b":"3","c":"5"},{"a":"2","b":"4","c":"6"}]`, since the first isn't valid js. – Robbie Speed Nov 23 '16 at 00:56
  • yes, you are right. I will update this right now. Also big thanks for the quick response, this works perfectly. – ThexBasic Nov 23 '16 at 01:09
  • Wouldn't `for in` and `for (let i=0; i – Bergi Nov 23 '16 at 01:15
  • @Bergi not using `for in` because it wouldn't provide the index, and basic `for` would add more boilerplate. However I do agree a `reduce` could be used instead. – Robbie Speed Nov 23 '16 at 01:20