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)?

8
  • "would be the "best" way"... if this question is holding you up in your progression, then I would strongly encourage you to read this: joelonsoftware.com/articles/fog0000000018.html Commented Nov 23, 2016 at 0: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. Commented Nov 23, 2016 at 0:26
  • 1
    Actually for-loops would be a quite good idea. Commented Nov 23, 2016 at 0:31
  • If you wanted, you could get quite fancy Commented Nov 23, 2016 at 0: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 :) Commented Nov 23, 2016 at 0:43

1 Answer 1

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;
  }, []);
}
Sign up to request clarification or add additional context in comments.

4 Comments

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.
yes, you are right. I will update this right now. Also big thanks for the quick response, this works perfectly.
Wouldn't for in and for (let i=0; i<data[key].length; i++) loops be even simpler?
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.