Home > other >  Create Javascript array using wildcards
Create Javascript array using wildcards

Time:07-07

I want to create an array of strings that contains discount values.

const discount = [
    '1% discount',
    '2% discount',
    '3% discount',
    '4% discount',
    '5% discount',
     ... ,
     ... ,
     ... ,
    '97% discount',
    '98% discount',
    '99% discount',
    '100% discount'
  ];

Is there any better and faster way to achieve this?

CodePudding user response:

const discount = Array.from({length: 100}, (_, index) => index   1   '% discount');
console.log(discount);

CodePudding user response:

One way to do it is using a for-loop:

array = []

for(let i = 1; i <= 100; i  ){
  array.push(`${i}% discount`)
}

console.log(array)

  • Related