2

I have a json similar to this one

 {      
    "id": "1",
    "month": "January",
    "type": "inc",
    "Value": "780.00",
    "year": "2018",
  },

 {      
    "id": "2",
    "month": "January",
    "type": "inc",
    "Value": "80.00",
    "year": "2018",
  },
 {      
    "id": "3",
    "month": "February",
    "type": "inc",
    "Value": "100.00",
    "year": "2018",
  },...

Now I need to get all the Value from the object for all the months, as you can see I may have more objects with the same month name. The closer I got to was creating 2 arrays 1 with the list of Months and 1 with the value but I got stuck, can someone lead me to the correct path?

The desired output would be to get an array like that ["January"=>1500, "February"=>2000...] or have 2 arrays, 1 with the list of months where there is income (I already have it) and the second the total income for these months, so it's like this: ["January", "February", "March"..] and the second one [1500, 2000, 300...]

1
  • 3
    Post the desired output and what you've tried so far. Commented Nov 2, 2018 at 14:44

6 Answers 6

5

You can use the function Array.prototype.reduce to sum each Value by month.

let arr =  [{          "id": "1",    "month": "January",    "type": "inc",    "Value": "780.00",    "year": "2018",  }, {          "id": "2",    "month": "January",    "type": "inc",    "Value": "80.00",    "year": "2018",  }, {          "id": "3",    "month": "February",    "type": "inc",    "Value": "100.00",    "year": "2018",  }],
    result = arr.reduce((a, {month, Value}) => {
      a[month] = (a[month] || 0) + +Value;
      return a;
    }, Object.create(null));
  
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

3 Comments

That's good, but I would need the total values in a month to be the sum, so January would be 860
@Radu033 Is it not possible to loop over those values and add them up?
wow, so simple, and nice thank you, and I had over 25 lines of code so far.
1

I actually can barely understand what you would like to achieve. Please provide some example. If I understood you correctly, you can use map function of js array to map each object to its Value.

let arr = [...];
console.log(arr.map(item => item.Value));

1 Comment

Sorry, I explained better in my question
1

You can do

    var fabuaryDate = yourdata
.filter(function(data) { return data.month == "February" })
.map(function(x){return {value: x.Value} })

1 Comment

yes but that will get it only for february, I need it for all months to put it in a chart
1

To get result in following format :

{
  jan : [1,2,3],
  feb : [3,4,5,6],
  april : [3,4,5]
}

do this :

var output = {}
arr.forEach(element => {
   if(!output[element.month]){
      output[month] = new Array();
   }
   output[month].push(element.value);
});

Comments

1

You can iterate the object and fill an array with the values of the field you want to extract, like so:

const data = [ {      
    "id": "1",
    "month": "January",
    "type": "inc",
    "Value": 780.00,
    "year": "2018",
  },
  {      
    "id": "2",
    "month": "January",
    "type": "inc",
    "Value": 80.00,
    "year": "2018",
  },
  {      
    "id": "3",
    "month": "February",
    "type": "inc",
    "Value": 100.00,
    "year": "2018",
  }];
  
let dataArray = data.reduce((accum, d) => {
  if(!accum[d.month]) accum[d.month] = 0;
  accum[d.month] += d.Value;
  return accum;
},{});
console.log(dataArray);

Comments

1

Although you don't seem to be clear enough with what have you tried here is an example of what you could do in order to read all the values inside the json.

function myFunction(item) {
   console.log(item.month + " with the value " + item.Value)
}

var jsonArray = [{"id": "1","month": "January", "type": "inc", "Value": "780.00", "year": "2018" }, { "id": "2",      "month": "January",  "type": "inc",   "Value": "80.00", "year": "2018"  }, { "id": "3", "month": "February",      "type": "inc", "Value": "100.00", "year": "2018" }];

jsonArray.forEach(myFunction);

Since you're working with an array of objects you must access to each of the objects in the array and then get the attribute that you require.

Hope this help, have a great day.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.