Home > database >  I would like to generate a array like this [ 1, 2, ..., 9, 10 ] in javascript
I would like to generate a array like this [ 1, 2, ..., 9, 10 ] in javascript

Time:04-26

if count is 4, arr will be [ 1, 2, 3, 4 ]

if count is greater than 4, arr will be like [ 1, 2, "...", 9, 10 ]

I tried this way, but output is [ 1, 2, "..." ]

const arr = [];

const count = 10;

for (let i = 0; i < count; i  ) {
    if (i >= 8) {
        arr.push(i   1);
        continue;
    }
    if (i >= 2) {
        arr.push("...");
        break;
    }

    arr.push(i   1);
}

CodePudding user response:

I'd write this as

let i=0;
for (; i < count && i < 2; i  ) {
    arr.push(i   1);
}
if (count > 4) {
    arr.push("…");
    i = count - 2;
}
for (; i < count; i  ) {
    arr.push(i   1);
}

CodePudding user response:

You can predefine it in the if as well and then use the loop.

let arr =

 [];
const count = 10;

if (count <= 4) {
  for (let i = 0; i < count; i  ) {
    arr.push(i);
  }
} else {
  arr = [1, 2, '...', count - 1, count];
}
console.log(arr);

JSFiddle

CodePudding user response:

From what I see from the OP's expected outputs/results it looks like the problem was more about creating kind of a sequence with ellipsis. In this case one could choose a much simpler approach.

Any sequence with a count lower than and equal to 4 will be created via Array.from and its optional mapFn parameter. For any count value which exceeds 4 there is no necessity of using any kind of loop; just create an array with the first two items which are always 1 and 2, followed by the ellipsis placeholder of '...', followed by the last two items which always have the values of count - 1 respectively count.

function createEllipsisSequence(count) {
  let list;
  if (count <= 4) {

    list = Array
      .from({ length: count }, (_, idx) => idx   1);
      
  } else {
    list = [1, 2, '...', count - 1, count];
  }
  return list;
}

console.log('count: 3 ...',
  createEllipsisSequence(3)
);
console.log('count: 4 ...',
  createEllipsisSequence(4)
);

console.log('count: 5 ...',
  createEllipsisSequence(5)
);
console.log('count: 6 ...',
  createEllipsisSequence(6)
);

console.log('count: 10 ...',
  createEllipsisSequence(10)
);
console.log('count: 11 ...',
  createEllipsisSequence(11)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

CodePudding user response:

You could define a function to which you send count and you would receive back the desired array. The function will return [] if count is less than 4.

const fun = count => Array(count === 4 ? 4 : count > 4 ? 5 : 0).fill('')
                     .map((e, i, a) => 
                         a.length === 4 || i < 2 ? i   1 : i === 2 ? "..." : i   6
                     );
                     
console.log( fun(0) );

console.log( fun(4) );

console.log( fun(6) );

Your code needs a few corrections. You can employ a loop as follows:

const arr = [];

const count = 10;

for (let i = 0; i < count; i  ) {
    if (i >= 8) {
        arr.push(i   1);
        continue;
    }
    if (count > 4) {
        if( i === 2 ) {
            arr.push("...");
        } else if( i < 2 ) {
            arr.push(i   1);
        }
        continue;
    }

    arr.push(i   1);
}

console.log( arr );

CodePudding user response:

You could handle the edges in one if and another case for the ...

let arr = [];

const count = 10;

for (let i = 0; i < count; i  ) {
  
  if (i < 2 || i >= count - 2) {
    arr.push(i   1)
  } else if (i === 2) {
    arr.push('...');
  }
}

console.log(arr)

CodePudding user response:

Two simle functions

  1. make an array from 1 to N
  2. ellipsis an array

const makeList = (size) => [...Array(size   1).keys()].slice(1);
const ellipsisList = (list) => (list.length <= 4) 
   ? list
   : [list.at(0), list.at(1), '...', list.at(-2), list.at(-1)];
   

console.log(ellipsisList(makeList(4)));
console.log(ellipsisList(makeList(6)));
.as-console-wrapper {max-height: 100% !important; top: 0}

  • Related