javascriptarraysjavascript-objects

Add up JSON strings to one variable | JavaScript


I have been trying to figure out how to merge several JSON strings into one.

My method is this:

if (observations[31]) { // Ignore this.
  document.getElementById("ajaxrainmo").textContent = observations[31].imperial.precipTotal + observations[30].imperial.precipTotal + observations[29].imperial.precipTotal + observations[28].imperial.precipTotal + observations[27].imperial.precipTotal + observations[26].imperial.precipTotal + observations[25].imperial.precipTotal + observations[24].imperial.precipTotal + observations[23].imperial.precipTotal + observations[22].imperial.precipTotal + observations[21].imperial.precipTotal + observations[20].imperial.precipTotal + observations[19].imperial.precipTotal + observations[18].imperial.precipTotal + observations[17].imperial.precipTotal + observations[16].imperial.precipTotal + observations[15].imperial.precipTotal + observations[14].imperial.precipTotal + observations[13].imperial.precipTotal + observations[12].imperial.precipTotal + observations[13].imperial.precipTotal + observations[11].imperial.precipTotal + observations[10].imperial.precipTotal + observations[9].imperial.precipTotal + observations[8].imperial.precipTotal + observations[7].imperial.precipTotal + observations[6].imperial.precipTotal + observations[5].imperial.precipTotal + observations[4].imperial.precipTotal + observations[3].imperial.precipTotal + observations[2].imperial.precipTotal + observations[1].imperial.precipTotal + observations[0].imperial.precipTotal + " in"
} // Adding up a bunch of rain totals to one number.

It works.. but it is very confusing and messy.. if there is a faster option avaliable, I could use it. Thanks.


Solution

  • if there is a faster option avaliable

    I don't think, you can achieve your goal faster, but you can write readable and maintainable code.

    I have been trying to figure out how to merge several JSON strings into one.

    Assuming the array has more elements and you want the first 32:

    const observations = [{imperial:{precipTotal:'a1'}},{imperial:{precipTotal:'b1'}},{imperial:{precipTotal:'c1'}},{imperial:{precipTotal:'d1'}},{imperial:{precipTotal:'e1'}},{imperial:{precipTotal:'a2'}},{imperial:{precipTotal:'b2'}},{imperial:{precipTotal:'c2'}},{imperial:{precipTotal:'d2'}},{imperial:{precipTotal:'e2'}},{imperial:{precipTotal:'a3'}},{imperial:{precipTotal:'b3'}},{imperial:{precipTotal:'c3'}},{imperial:{precipTotal:'d3'}},{imperial:{precipTotal:'e3'}},{imperial:{precipTotal:'a4'}},{imperial:{precipTotal:'b4'}},{imperial:{precipTotal:'c4'}},{imperial:{precipTotal:'d4'}},{imperial:{precipTotal:'e4'}},{imperial:{precipTotal:'a5'}},{imperial:{precipTotal:'b5'}},{imperial:{precipTotal:'c5'}},{imperial:{precipTotal:'d5'}},{imperial:{precipTotal:'e5'}},{imperial:{precipTotal:'a6'}},{imperial:{precipTotal:'b6'}},{imperial:{precipTotal:'c6'}},{imperial:{precipTotal:'d6'}},{imperial:{precipTotal:'e6'}},{imperial:{precipTotal:'a7'}},{imperial:{precipTotal:'b7'}},{imperial:{precipTotal:'c7'}},{imperial:{precipTotal:'d7'}},{imperial:{precipTotal:'e7'}},{imperial:{precipTotal:'a8'}},{imperial:{precipTotal:'b8'}},{imperial:{precipTotal:'c8'}},{imperial:{precipTotal:'d8'}},{imperial:{precipTotal:'e8'}}];
    
    const result = observations.slice(0, 32).map(o => o.imperial.precipTotal).reverse().join('') + ' in';
    
    console.log(result);

    slice(0, 32): take the first 32 elements (you don't need it, if you take all array elements)

    map(o => o.imperial.precipTotal): map each array element to the precipTotal string

    reverse(): reverse the strings

    join(''): join the strings to one string without separator

    Assuming, I misunderstood your question and precipTotal contains numbers that you want to sum up, I would go a similar way.

    const observations = [{imperial:{precipTotal:11}},{imperial:{precipTotal:21}},{imperial:{precipTotal:31}},{imperial:{precipTotal:41}},{imperial:{precipTotal:51}},{imperial:{precipTotal:12}},{imperial:{precipTotal:22}},{imperial:{precipTotal:32}},{imperial:{precipTotal:42}},{imperial:{precipTotal:52}},{imperial:{precipTotal:13}},{imperial:{precipTotal:23}},{imperial:{precipTotal:33}},{imperial:{precipTotal:43}},{imperial:{precipTotal:53}},{imperial:{precipTotal:14}},{imperial:{precipTotal:24}},{imperial:{precipTotal:34}},{imperial:{precipTotal:44}},{imperial:{precipTotal:54}},{imperial:{precipTotal:15}},{imperial:{precipTotal:25}},{imperial:{precipTotal:35}},{imperial:{precipTotal:45}},{imperial:{precipTotal:55}},{imperial:{precipTotal:16}},{imperial:{precipTotal:26}},{imperial:{precipTotal:36}},{imperial:{precipTotal:46}},{imperial:{precipTotal:56}},{imperial:{precipTotal:17}},{imperial:{precipTotal:27}},{imperial:{precipTotal:37}},{imperial:{precipTotal:47}},{imperial:{precipTotal:57}},{imperial:{precipTotal:18}},{imperial:{precipTotal:28}},{imperial:{precipTotal:38}},{imperial:{precipTotal:48}},{imperial:{precipTotal:58}}];
    
    const result = observations.slice(0, 32).map(o => o.imperial.precipTotal).reduce((acc, el) => acc + el) + ' in';
    
    console.log(result);

    slice(0, 32): take the first 32 elements (you don't need it, if you take all array elements)

    map(o => o.imperial.precipTotal): map each array element to the precipTotal string

    reduce((acc, el) => acc + el): sum up the values