1

So, let's say I have an array of months. I want it to say what the item position is and the item name, and create a sort or list instead of having it be "january,febuary,march,april". Example below

``ITEM_POSITION`` - MONTH_NAME
``0`` - January
``1`` - Feburary
``2`` - March
``3`` - April

and so on...

How would I list the months like this?

(The Back Quote is intentional) (Sorry for bad explanation)

1
  • Put it in an array and you are done. index->month. First link google came up with: w3schools.com/js/js_arrays.asp Commented Jun 28, 2020 at 6:29

3 Answers 3

1

Are you familiar with for loops? How about something like the following:

const months = ["january", "february", "march", "april"];
// print your header here
// ...
for (let idx = 0; idx < months.length; idx++) {
  // print desired info for each month here
  // this isn't the exact format you want, but here's a hint
  console.log(idx + " - " + months[idx]);
}
// print any "footer" here
// ...

This is not the only way to achieve what you want, but is probably the most straightforward.

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

4 Comments

I want to be able to collect that as one whole variable and not separate things.
If I understand you correctly, that should be achievable by creating another variable and appending to it in the loop. E.g. let monthMsg = '' at the top, then monthMsg += <format you want> inside the loop instead of console.log
So I tried monthMsg += idx + " - " + months[idx], and it would just give me 0 - january1 - february2 - march3 - april Any ideas?
NVM XD, it was just that it was bunched together. Thank you!
0

You can store months in an array. Each element has its own index according to its position. Another way is to create an object and asign key/value pairs.

//ARRAY
let arr = ['January', 'February', 'March', 'April'];

//Access each arr element
arr[0];  // 'January'
arr[1];  // 'February'

//Iterate arr elements
arr.map(x => console.log(x));   // map method (iterates each element) and logs in console

//Sort
console.log(arr.sort());        // sort alphabetically


//OBJECT
let obj = {0: 'January',
           1: 'February',
           2: 'March',
           3: 'April'
           };

console.log(obj);

//Access obj properties
obj[0];     // 'January';
obj[1];     // 'February'

Comments

0

I assume you're trying to create an object like structure.

like:

{ item_position: 0, month: 'January' }

you can do it like this (Even though is really redundant):

const months = ['january', 'february', 'march'];
const months_list = [];

months.forEach((month, index) => {
     months_list.push(
         {
             "ITEM_POSITION" : index,
             "MONTH_NAME" : month
         }
     )
})

months_list.forEach(item => {
     console.log(item.ITEM_POSITION, item.MONTH_NAME) // 0, 'january'
})

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.