0

can I give to empty array certain length that, that array will take only that much length of elements. for example:

 var emptyArray = []; // but I need this array takes only 10 elements
 for (var i = 0; i < 50; i++) {
     emptyArray.push(i);
 }
 console.log(emptyArray.length) // I want to see 10
 console.log(emptyArray) // [0,1,2,3,4,5,6,7,8,9]
4
  • 2
    No, but you can check its length before pushing, optionally wrapping that check in a function or object. Commented Apr 18, 2018 at 21:26
  • You can define your own object type that acts like an array, but limits its size. Commented Apr 18, 2018 at 21:27
  • 1
    What is the behavior you want on the 11th push? An error? Silence? Push earlier elements off the other end? All of these are doable, but you need to decide which is correct for your application. Commented Apr 18, 2018 at 21:30
  • @Mark_M: Silence – see the example in the post Commented Apr 18, 2018 at 21:31

3 Answers 3

3

You can try Object.seal(). Note that in this example, I'm not using push() since that will throw an error, but assigning values since the values in the fixed length Array are mutable

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/seal

var emptyArray = new Array(10).fill(0);

Object.seal(emptyArray);

 for (var i = 0; i < 50; i++) {
     emptyArray[i] = i;
 }
 console.log(emptyArray.length) // I want to see 10
 console.log(emptyArray) // [0,1,2,3,4,5,6,7,8,9]

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

1 Comment

This would be elegant, but it’ll throw in strict mode.
0

Another approach is to use a Proxy:

function limitedArray(length) {
  return new Proxy([], {
    set: (target, prop, value) => {
      if (Number(prop) < length) {
        target[prop] = value;
      }
      
      return true;
    }
  });
}

var tenArray = limitedArray(10);

for (let i = 0; i < 50; ++i) {
  tenArray.push(i);
}

console.log(Array.from(tenArray)); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

You can use a different set method instead, if you want it to throw an error when the array’s length is about to be exceeded:

set: (target, prop, value) => {
  if(!isNaN(prop) && Number(prop) >= length){
    return false;
  }

  if (Number(prop) < length) {
    target[prop] = value;
  }

  return true;
}

Note though, that handling deletion of elements requires a bit more work.

2 Comments

Depends on where the code is being used though right? I don't think you can use transpiling or polyfilling for Proxy.
Proxy does not have Internet Explorer support right? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
-1

Not directly as you want, however you can have something similar :

var emptyArray = Array.from({length:50}, (v,i) => undefined);
for(let i=0; i<emptyArray;i++){
    emptyArray.push(i);
}

Basically you set up your array with undefined values with a certain length, and then loop until you reach the length.

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.