0

I am trying to store the max and min temperature of a year.

So I've got an array and an object like this.

var Months =['January','February','March','April','May','June','July','August','September','October','November','December'];  

result[0] = {January: 26.2, February: 25.9, March: 25.7, April: 23.4, May: 19.3, June: 15.9, July: 15.4, August: 16, September: 21.3, October: 24.8, November: 26.6, December: 26.6,Type: "Min"}

result[1] = {January: 36.3, February: 35.9, March: 36, April: 36.7, May: 34.2, June: 31.4, July: 32, August: 34, September: 37.8, October: 39.5, November: 39.8, December: 37.8,Type: "Max"}.

This is the data I have and now I want two different arrays that store data in a proper order. (From Jan - Dec)

For example:

var min = [26.2,25.9,25.7,23.4,19.3,15.9,15.4,16,21.3,24.8,26.6,26.6]
var max = [36.3,35.9,36,36.7,34.2,31.4,32,34,37.8,39.5,39.8,37.8]

How can I extract that data from the object and make sure the order is the same with the "Months"? Also I need to remove the "Type" of Min/Max in my new arrays.

1
  • And the problem is? Iterate over the months and use them to extract the values from the objects... Commented Aug 6, 2018 at 11:46

3 Answers 3

5

You can simply use .map() method to map the relevant month values from your result objects and assign them to your min and max variables.

This is how should be your code:

var [min, max] = result.map(r => Months.map(m => {
  return r[m]
}));

Demo:

var Months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

var result = [];


result[0] = {January: 26.2, February: 25.9, March: 25.7, April: 23.4, May: 19.3, June: 15.9, July: 15.4, August: 16, September: 21.3, October: 24.8, November: 26.6, December: 26.6,Type: "Min"};

result[1] = {January: 36.3, February: 35.9, March: 36, April: 36.7, May: 34.2, June: 31.4, July: 32, August: 34, September: 37.8, October: 39.5, November: 39.8, December: 37.8,Type: "Max"};

var [min, max] = result.map(r => Months.map(m => {
  return r[m]
}));

console.log(min);
console.log(max);

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

Comments

1

You do not need any loop. Simply use Object.values() and pop() so that the last element is removed from array which is Type value.

var Months  = ['January','February','March','April','May','June','July','August','September','October','November','December'];  

var result = [{January: 26.2, February: 25.9, March: 25.7, April: 23.4, May: 19.3, June: 15.9, July: 15.4, August: 16, September: 21.3, October: 24.8, November: 26.6, December: 26.6,Type: "Min"},
 {January: 36.3, February: 35.9, March: 36, April: 36.7, May: 34.2, June: 31.4, July: 32, August: 34, September: 37.8, October: 39.5, November: 39.8, December: 37.8,Type: "Max"}];
 
 var min = Object.values(result[0]);
 min.pop();
 var max = Object.values(result[1]);
 max.pop();
 console.log(min);
 console.log(max);

Comments

0

You could use the ordered elements of the months array to sequentially access the appropriate month keys of each result object. Since your months array doesn't include a Type element, that key will be ignored.

var Months =['January','February','March','April','May','June','July','August','September','October','November','December'];
var result = []
result[0] = {January: 26.2, February: 25.9, March: 25.7, April: 23.4, May: 19.3, June: 15.9, July: 15.4, August: 16, September: 21.3, October: 24.8, November: 26.6, December: 26.6,Type: "Min"}
result[1] = {January: 36.3, February: 35.9, March: 36, April: 36.7, May: 34.2, June: 31.4, July: 32, August: 34, September: 37.8, October: 39.5, November: 39.8, December: 37.8,Type: "Max"}

var min = []
var max = []

for (let month of Months) {
  min.push(result[0][month])
  max.push(result[1][month])
}

console.log(min, max)

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.