0

I want to show some data depending on which month it is. I have an array from 0 to 11. I'm confused on how to do this because there are multiple arrays inside of the object and it's scaring me lol

[
  {
    l: false,
    u: 'AU',
    n: 'Adelaide',
    rain: [
      22.4091,
      18,
      26.0909,
      42.875,
      56.9205,
      68.0568,
      75.4205,
      66.3636,
      58.8977,
      48.75,
      28.2159,
      27.9545,
    ],
  },
  {
    l: false,
    u: 'AU',
    n: 'A Different Place',
    rain: [
      22.4091,
      18,
      26.0909,
      42.875,
      56.9205,
      68.0568,
      75.4205,
      66.3636,
      58.8977,
      48.75,
      28.2159,
      27.9545,
    ],
  },
];

I have a variable that I can reference like this: getMonth(new Date(this.props.startDate))

0

Can I do something like this?

forEach(data[x].rain[getMonth(new Date(this.props.startDate))])

I'd like to replace the existing rain array with the data of the corresponding month. So for example if it was January (0) I'd like data[1].rain = 22.4091

expected output if (getMonth = 0):

[
  {
    l: false,
    u: 'AU',
    n: 'Adelaide',
    rain: 22.4091
  },
  {
    l: false,
    u: 'AU',
    n: 'A Different Place',
    rain: 22.4091
    ],
  },
];
3
  • What do you want the output to be? It's confusing at the moment Commented May 27, 2019 at 14:34
  • to replace the array with the integer which is an index of month Commented May 27, 2019 at 14:38
  • if value will change based on month, you can't put 0, see below answer Commented May 27, 2019 at 14:51

2 Answers 2

1

You can use map(). Return an object from map() with all properties same as previous but change rain to the the first element of it.

 let arr = [ { l: false, u: 'AU', n: 'Adelaide', rain: [ 22.4091, 18, 26.0909, 42.875, 56.9205, 68.0568, 75.4205, 66.3636, 58.8977, 48.75, 28.2159, 27.9545, ], }, { l: false, u: 'AU', n: 'A Different Place', rain: [ 22.4091, 18, 26.0909, 42.875, 56.9205, 68.0568, 75.4205, 66.3636, 58.8977, 48.75, 28.2159, 27.9545, ], }, ];

const res = arr.map(x => ({...x,rain:x.rain[0]}))
console.log(res)

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

2 Comments

so I just put [getMonth(new Date(this.props.startDate))] instead of [0]?
@jaksco Yes try that and tell me if it works or not.
0

you can do this

let data = [ { l: false, u: 'AU', n: 'Adelaide', rain: [ 22.4091, 18, 26.0909, 42.875, 56.9205, 68.0568, 75.4205, 66.3636, 58.8977, 48.75, 28.2159, 27.9545, ], }, { l: false, u: 'AU', n: 'A Different Place', rain: [ 22.4091, 18, 26.0909, 42.875, 56.9205, 68.0568, 75.4205, 66.3636, 58.8977, 48.75, 28.2159, 27.9545, ], }, ];

const res = data.map(x => ({...x,rain:x.rain[ (new Date(this.props.startDate).getMonth()) ]}))
console.log(res)

if this is already date then this.props.startDate.getMonth() else convert as above

2 Comments

cool. is there a difference in memory between .getMonth() and the way I was doing it? yours is working. I guess that means I don't need to import getMonth?
getMonth is default function of date in javascript, no need any imports

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.