0

I figure I shouldn't be having trouble with this, but I am. I am trying to switch up the syntax/variables of a JSON object to match a certain parameters.

Here is the JSON I am working with:

{  
   "name":"BHPhotovideo",
   "prices":[  
      {  
         "price":"799.00",
         "createdAt":"2017-07-23T16:17:11.000Z",
         "updatedAt":"2017-07-23T17:21:41.000Z"
      },
      {  
         "price":"770.00",
         "createdAt":"2017-07-21T16:17:11.000Z",
         "updatedAt":"2017-07-23T16:17:11.000Z"
      },
      {  
         "price":"599.00",
         "createdAt":"2017-07-19T16:17:11.000Z",
         "updatedAt":"2017-07-22T16:17:11.000Z"
      },
      {  
         "price":"920.00",
         "createdAt":"2017-07-22T16:17:11.000Z",
         "updatedAt":"2017-07-22T16:17:11.000Z"
      }
   ]
},
etc...

I am just trying to get the data to be formatted like this:

{  
   "label":"BHPhotoVideo", // Same as name
   "data":[  
      {  
         "x":"2017-07-23T16:17:11.000Z", // Same as createdAt
         "y":799 // Same as price
      },
      {  
         "x":"2017-07-21T16:17:11.000Z",
         "y":770
      },
      {  
         "x":"2017-07-19T16:17:11.000Z",
         "y":599
      },
      {  
         "x":"2017-07-22T16:17:11.000Z",
         "y":920
      }
   ]
},
etc...

The amount of these objects are dynamic/subject to change, I've been making a mess out of foreach loops and trying to piece this together. I keep coming into errors, what's the best way to approach this?

Miles Collier
  • 370
  • 2
  • 17

3 Answers3

2

What about this ?

data.map(
   (item) => ({
       "label":"BHPhotoVideo", // Same as name
       "data": item.prices.map(nested => ( {  
         "x":nested.createdAt,
         "y":nested.price
      }))
   })
)
Anatoly Strashkevich
  • 1,834
  • 4
  • 16
  • 32
  • Wow, data mapping is a thing. No idea how I missed that. This is perfect, changed it a bit and it's perfect for my application. THANKS! – Miles Collier Jul 23 '17 at 19:42
0

Did you want the y values to be integers?

var ar = [
   {
      "name":"BHPhotovideo",
      "prices":[
         {
            "price":"799.00",
            "createdAt":"2017-07-23T16:17:11.000Z",
            "updatedAt":"2017-07-23T17:21:41.000Z"
         },
         {
            "price":"770.00",
            "createdAt":"2017-07-21T16:17:11.000Z",
            "updatedAt":"2017-07-23T16:17:11.000Z"
         },
         {
            "price":"599.00",
            "createdAt":"2017-07-19T16:17:11.000Z",
            "updatedAt":"2017-07-22T16:17:11.000Z"
         },
         {
            "price":"920.00",
            "createdAt":"2017-07-22T16:17:11.000Z",
            "updatedAt":"2017-07-22T16:17:11.000Z"
         }
      ]
   },
   {
      "name":"Adorama",
      "prices":[
         {
            "price":"799.00",
            "createdAt":"2017-07-22T16:17:11.000Z",
            "updatedAt":"2017-07-23T17:21:41.000Z"
         },
         {
            "price":"799.00",
            "createdAt":"2017-07-20T16:17:11.000Z",
            "updatedAt":"2017-07-23T16:17:11.000Z"
         },
         {
            "price":"810.00",
            "createdAt":"2017-07-18T16:17:11.000Z",
            "updatedAt":"2017-07-22T16:17:11.000Z"
         },
         {
            "price":"799.00",
            "createdAt":"2017-07-17T16:17:11.000Z",
            "updatedAt":"2017-07-22T16:17:11.000Z"
         }
      ]
   }
];


var out  = ar.map( function(a) {
    return  {
        "label" : a.name,
        "prices" : a.prices.map( function(aa) { return {x: aa.createdAt, y: aa.price} })
    }
});
console.log( out );
Jarek Kulikowski
  • 1,399
  • 8
  • 9
0

map over the original array returning a changed object; returning the name, and a new array from using map over the prices.

const obj2 = obj.map((item) => {
  return {
    label: item.name,
    data: item.prices.map((data) => {
      return {
        x: data.createdAt,
        y: data.price
      }
    })
  }
});

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95