-2

I have an array of object. Array length is 12. I need to add months to each object, whats the best way to add months

  array = [ {name:"aa"},{name:"bb"}, {name:"cc"}.......]. 

Result I want

array = [ {monthName: 'January', name:"aa"},{monthName: 'February',name:"bb"}, {monthName: 'March',name:"cc"}............]. 
2
  • Does this answer your question? Add property to all objects in array Commented Feb 27, 2020 at 20:48
  • Do i need to write each month manually? I am sure using JS we won't need it Commented Feb 27, 2020 at 20:50

5 Answers 5

1

Since the month names are immutable and you already know the array is exactly length 12, simply do it manually, one by one. There are fancier ways to do it but none are as easy to understand or as efficient with resources.

array[0].monthName = 'January';
array[1].monthName = 'February';
array[2].monthName = 'March';
...
Sign up to request clarification or add additional context in comments.

2 Comments

No, There should be another way to do it
For sure, senior developers wont push my code if I manually write months. I need to add months from Jan to Dec
1

store all the months in an array months = ['January', 'February', 'March'.....]

loop through your array and match indexes

array.map((x, index) => {
  if(months[index]) {
    x.monthName = months[index];
  }
  return x;
}

4 Comments

Any other way to do it?
I thinks its not good idea to write months, there should be some method like getFullMonth
Why? Are you worried that the names of the months will change? Or that they're going to add a new month to the calendar?
there is a way like date.toLocaleString('default', { month: 'long' }); but you will need the date in that case; is that what you are looking for?
1

You can add an array of months and without mutating the array you can create another variable based on your first array and add the month as prop:

let array = [{
  name: "aa"
}, {
  name: "bb"
}, {
  name: "cc"
}];

let months = ["JAN", "FEB", "MAR"]
const res = array.map((el, index) => ({
  name: el.name,
  month: months[index]
}));



console.log(res)

1 Comment

@developer exactly
1

First, you have to declare array which will have month as below array

let month = ['January',February', .... ];

Now you have to iterate source array i.e array and push month property for each object. Code snippets provided below.

array.forEach(element,index)=>{
     element['monthName'] = month[index]
}

Comments

0

Define an array of months yourself or get it from a library like moment.js.

This way you are ready to work with dates later if you need much easier. You also get localization (no easy task) and short names if you prefer (e.g. 'Jan' instead of 'January').

var array  = [{name:"aa"}, {name:"bb"}, {name:"cc"}];
var months = moment.months(); // or .monthsShort();

array.forEach((e, idx) => {
   if (months[idx]) e.monthName = months[idx];
});

console.log(array);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment-with-locales.min.js"></script>

Here is the documentation on months() and monthsShort().

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.