user3142695
user3142695

Reputation: 17372

How to fill an array with object containing ascending stringified numbers?

I need to generate an object array which has three keys with string values from 1 to 31. The strings should have two characters, which means 1-9 should get a 0 in front.

const days = [
  { key: '01', value: '01', text: '01' },
  { key: '02', value: '02', text: '02' },
  { key: '03', value: '03', text: '03' },
  ...
  { key: '30', value: '30', text: '30' },
  { key: '31', value: '31', text: '31' }
]

I think it should be done a bit shorter and smarter, then the solution which I came up with:

const days = Array.from({ length: 31 }, (v, k) => {
  return { 
    key: k > 10 ? (k + 1).toString() : '0' + (k + 1),
    value: k > 10 ? (k + 1).toString() : '0' + (k + 1),
    text: k > 10 ? (k + 1).toString() : '0' + (k + 1)
  }
})

And what to do if I don't start at value 1? For example the same array for the years from 1950 until now (in reverse order):

const years = [
  { key: '2018', value: '2018', text: '2018' },
  ...
  { key: '1950', value: '1950', text: '1950' }
]

Upvotes: 1

Views: 44

Answers (3)

Slai
Slai

Reputation: 22896

const days = Array.from({ length: 31 }, (v, i) => 
  ({ key: v = (++i > 9 ? '' : '0') + i, value: v, text: v }) );

const years = Array.from({ length: 2018 - 1950 + 1 }, (v, i) => 
  ({ key: v = 2018 - i + '', value: v, text: v }) );

console.log( JSON.stringify( days  ).replace(/},/g, '},\n ') );
console.log( JSON.stringify( years ).replace(/},/g, '},\n ') );

Upvotes: 0

Eddie
Eddie

Reputation: 26854

You make it simple, you can use padStart()

const days = Array.from({length: 31}, (v, k) => {
  let o = (k + 1).toString().padStart(2, '0');
  return {key: o,value: o,text: o}
});

console.log(days);

Regarding the year, the same structure but instead of adding 1, add the start year.

const years = Array.from({length: 20}, (v, k) => {
  let o = (k + 2000);
  return {key: o,value: o,text: o}
});

console.log(years);

Doc: padStart()

Upvotes: 2

T.J. Crowder
T.J. Crowder

Reputation: 1075885

I think it should be done a bit shorter and smarter...

Well, you can certainly repeat yourself a lot less:

const days = Array.from({ length: 31 }, (v, k) => {
  const value = k > 10 ? (k + 1).toString() : '0' + (k + 1);
  return { 
    key: value,
    value,
    text: value
  };
});

Live Example:

const days = Array.from({ length: 31 }, (v, k) => {
  const value = k > 10 ? (k + 1).toString() : '0' + (k + 1);
  return { 
    key: value,
    value,
    text: value
  };
});
console.log(days);
.as-console-wrapper {
  max-height: 100% !important;
}

And what to do if I don't start at value 1?

Just add in the value you want:

const startYear = 2018;
const years = Array.from({ length: 5 }, (v, k) => {
  const value = String(k + startYear);
  return { 
    key: value,
    value,
    text: value
  };
});

Live Example:

const startYear = 2018;
const years = Array.from({ length: 5 }, (v, k) => {
  const value = String(k + startYear);
  return { 
    key: value,
    value,
    text: value
  };
});
console.log(years);
.as-console-wrapper {
  max-height: 100% !important;
}

Naturally, wrap that in a function if you need something reusable.

Upvotes: 0

Related Questions