0

I am actually trying to display canvas.js chart using this json dynamically.

I have this result:

[{"s":40,"ProductName":"yoga pants"},
{"s":20,"ProductName":"trousers"},{"s":16,"ProductName":"shirts"},
{"s":10,"ProductName":"pants"},{"s":7,"ProductName":"RED"},
{"s":5,"ProductName":"Tank Top"}] 

…which is a json string

I am getting this:

[{40: "yoga pants", x: 0},
{20: "trousers", x: 1},
{16: "shirts", x: 2},
{10: "pants", x: 3},
{7: "RED", x: 4},
{5: "Tank Top", x: 5}]

But i do not want this extra x: value. I am using this code:

var result1 = JSON.parse(result);
var reformattedArray = result1.map(function (o) 
{ 
  var obj = {}; 
  obj[o.s] = o.ProductName;
  return obj; 
});

Thanks in advance

Mark
  • 90,562
  • 7
  • 108
  • 148
  • 1
    Code shown won't set a property `x` on the mapped objects – charlietfl Nov 09 '17 at 02:31
  • You say "I have this result" and "I am getting this" - is the first one your *input* and the second one the incorrect *output*, where actually you want output like that except without the `x` property? The code shown won't add that `x` property. – nnnnnn Nov 09 '17 at 02:33
  • This problem is not reproducible with the code you are showing. – Mark Nov 09 '17 at 02:36
  • Possible duplicate of [How do I remove a property from a JavaScript object?](https://stackoverflow.com/questions/208105/how-do-i-remove-a-property-from-a-javascript-object) – Daniel B Nov 09 '17 at 03:51

1 Answers1

0

It appears to me that what is really happening is that your reformattedArray is fine, but you are subsequently piping it through charting software that is assigning the x: property, effectively indexing your data for the x axis of a chart.

Is it possible that the result you are reporting is not the result of your map, but rather the result of an additional step?

Haim Zamir
  • 351
  • 2
  • 5