0

I have a JSON file which i load into my javascript var

Before plotting my data:

{
    "Month": "September",
    "Value": 1681937,
    "Year": 2013
},
{
    "Month": "April",
    "Value": 2138286,
    "Year": 2014
},

I need to replace the label "Value" with "Value+Year" which will look something like so: "Value2013" for September and "Value2014" for April.

From StackOverflow I understand this should be doable in javascript but i don't understand how to access for every entry the value of Year and replace the Label Value with Value+Year

Thank you in advance.

Jose Luis
  • 3,307
  • 3
  • 36
  • 53
NoobTom
  • 555
  • 1
  • 9
  • 21
  • What *exactly* are you having problems with? I assume you know how to iterate over an array? You basically have to combine [Is it possible to add dynamically named properties to JavaScript object?](http://stackoverflow.com/q/1184123/218196) with [Renaming a field in an object](http://stackoverflow.com/q/14789405/218196). – Felix Kling Jun 10 '14 at 15:05

2 Answers2

1

What about this ?

var my = [{
    "Month": "September",
    "Value": 1681937,
    "Year": 2013
}, {
    "Month": "April",
    "Value": 2138286,
    "Year": 2014
}];

console.log('my (before) :', my);

for(var i=0; i<my.length; i++) {
    my[i]["Value" + my[i].Year] = my[i].Value;
    delete my[i]["Value"];
}

console.log('my (after) :', my);

See the console of the jsFiddle to choose the one you need:

Result will be :

[{
    "Month": "September",
    "Value2013": 1681937,
    "Year": 2013
}, {
    "Month": "April",
    "Value2014": 2138286,
    "Year": 2014
}]

EDIT : You can see the different common possibilites and their performance results here : http://jsperf.com/jquery-each-vs-underscore-each-vs-for-loops/18

M'sieur Toph'
  • 2,534
  • 1
  • 21
  • 34
  • Thank you it works. How can you write JS so fast -.- – NoobTom Jun 10 '14 at 15:38
  • Hello, I mark your as answer, however I ended up using the _ below. The reason I mark your as a valid answer is because doesn't make use of external lib. If anyone feels differently comment below – NoobTom Jun 11 '14 at 10:52
  • Thx. I understand your choice. But according to this specific usage, it is way-faster to use the native javascript `for..in`. See this jsPerf to understand : http://jsperf.com/jquery-each-vs-underscore-each-vs-for-loops/4. Jquery or Underscore `.each` are really slower than the other solutions. You should never use them this way. – M'sieur Toph' Jun 11 '14 at 14:05
  • By the way, I figured out that `for..in` is not that fast that I tought. It is better to use `for(var i=0; i – M'sieur Toph' Jun 11 '14 at 14:14
  • If i save my json in a file say: file.json can i load it and pass it to your function? I tried: var chartDataSource = $.getJSON( "file.json"); but it doesn't seem to work because the data are stored in some complex object. I tried chartDataSource.array but got nowhere – NoobTom Jun 11 '14 at 15:26
  • Sure. But I you will need to parse it before. (with stuff like `JSON.parse()`). – M'sieur Toph' Jun 11 '14 at 15:29
  • This is an other issue. You should post a new question about your `getJSON()` that does not work as expected. Put the content of the file and js-code you're trying to execute... – M'sieur Toph' Jun 12 '14 at 07:31
1

I reccomend using underscore to achieve this smoothly.

Given an array of objects

var myArray=[{
    "Month": "September",
    "Value": 1681937,
    "Year": 2013
},
{
    "Month": "April",
    "Value": 2138286,
    "Year": 2014
}];

Your replacement should be:

_.each(myArray,function(element) { 
    element['Value'+element.Year]=element.Value; 
    delete element.Value; 
});

Resulting array is

[{
    "Month": "September",
    "Value2013": 1681937,
    "Year": 2013
},
{
    "Month": "April",
    "Value2014": 2138286,
    "Year": 2014
}];
ffflabs
  • 17,166
  • 5
  • 51
  • 77