-1

I have a JSON object that contains two arrays. I want to convert those array elements into individual elements in the JSON object.
I need this for an input into a D3 function in javascript.

My object now:

{
  0:{
      start:"2016-01-01",
      values:[10,11,12],
      names:["a","b","c"]
     },
  1:{
      start:"2016-01-02",
      values:[25,23,50],
      names:["a","b","c"]
     }
}

Converted object:

{
  0:{
      start:"2016-01-01",
      a:10,
      b:11,
      c:12
     },
  1:{
      start:"2016-01-02",
      a:25,
      b:23,
      c:50
    }
}

The number of variables can vary. It won't always be 3.

Any help appreciated, Thanks

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 4
    Welcome to StackOverflow! Have you tried anything so far? StackOverflow isn't a free code-writing service and expects you to [try to solve your own problem first](https://meta.stackoverflow.com/q/261592/1823841). Please update your question to show what you have already tried, showcasing a **specific** problem you are facing in a [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve). For further information, please see [how to ask a good question](https://stackoverflow.com/help/how-to-ask), and take the [tour of the site](https://stackoverflow.com/tour). – palaѕн Mar 21 '18 at 16:06
  • 4
    JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Mar 21 '18 at 16:06
  • I am just commenting, instead of answering, because i think that you did not even try to get a solution... but to point you in the right direction: use some javascript to (probably the simplest way: convert your json object into an array, and) iterate trough your object and create the new structure you desire. when you are done, you merge everything. – Iamnino Mar 21 '18 at 16:13
  • @palaѕн I am able to find a solution when the number of array elements are fixed. I don't know how to solve it when the number of elements vary. – amateurcoder Mar 21 '18 at 16:23
  • @amateurcoder Why does the number have to be fixed? Use `obj[i].names.length` to get the number of names in the current element of the object. – Barmar Mar 21 '18 at 16:31
  • @amateurcoder please add the code that you have so that we may guide you towards a solution – phuzi Mar 21 '18 at 16:31
  • @amateurcoder You can also use `obj[i].names.forEach()` to loop over all the names in the current element. – Barmar Mar 21 '18 at 16:33
  • @Barmar I I am using .length to get the length of the array. Once I have that, how do I dynamically declare variables for each array position? – amateurcoder Mar 21 '18 at 16:55
  • @amateurcoder There are no variables in your question, just dynamically-named properties. See https://stackoverflow.com/questions/17832583/create-an-object-with-dynamic-property-names – Barmar Mar 21 '18 at 16:56

2 Answers2

0

I know this not the best but this is one of the way to achieve what you want.

x={
  0:{
      start:"2016-01-01",
      values:[10,11,12],
      names:["a","b","c"]
     },
  1:{
      start:"2016-01-02",
      values:[25,23,50],
      names:["a","b","c"]
     }
}
for(y in x){
    if(x.hasOwnProperty(y)){
        x[y].names.forEach((key, i) => x[y][key] = x[y].values[i]);
        delete x[y].names;
        delete x[y].values;
    }
}

console.log(x);
yajiv
  • 2,901
  • 2
  • 15
  • 25
0

var array = {
  0:{
      start:"2016-01-01",
      values:[10,11,12],
      names:["a","b","c"]
     },
  1:{
      start:"2016-01-02",
      values:[25,23,50],
      names:["x","y","z"]
     }
}

convertArrayProp = ( newObject ) => {
  Object.keys( array ).map( ( key ) => {
    newObject[key] = {};
    newObject[key]['start'] = array[key]['start'];
    array[key]['names'].map( ( name, index ) => newObject[key][name] = array[key]['values'][index]);
  });
  return newObject;
}
console.log(convertArrayProp({}));
Dharman
  • 30,962
  • 25
  • 85
  • 135