0

const arrPassword = []
const passarrayLength = 5


function addPassword(passwd) {
  if(arrPassword.length === passarrayLength)
  {
    arrPassword.shift()
  }

  arrPassword.push(passwd)
  console.log(arrPassword.length)
  console.log(arrPassword)
}

addPassword('Pass')
addPassword('Pass2')
addPassword('Pass3')
addPassword('Pass4')
addPassword('Pass5')
addPassword('Pass6')
addPassword('Pass7')
addPassword('Pass8')
addPassword('Pass9')
addPassword('Pass10')

I have a few cases where I want to store objects like user password history in an Array of objects to ensure he has not used the password in the last 5 times for example. My question is can I specify an array of objects with a size of 5 and then just push new passwords to array and any object in the array above size set would be discarded ? Or do I have to do this my self where I count the objects in my Array and if it is = max size I pop the oldest one before I push the new object to array ? Based on the research I did typescript or javascript does not have a fixed array size, I can specify a array of 3 5 objects but will need to assign all 5 and even so the push would make it 6 objects as there is no limit. So what would be the best approach to handle this ?

I included some basic concept i cam up with

0

2 Answers 2

2

Can I specify an array of objects with a size of 5 and then just push new passwords to array and any object in the array above size set would be discarded?

Nope. Arrays in javascript do not have a maximum size, so there is no "built in" way to do this.

Or do I have to do this my self where I count the objects in my Array and if it is = max size I pop the oldest one before I push the new object to array?

Yep, that's exactly right. It shouldn't be too hard to make a little class that handles this logic.

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

Comments

1

When i need a functionality and there happens to be no such a functionality, the first thing that i think is "what am i missing?".

In this particular case all you need to do is to take the last passarrayLength many items from your arrPassword array and reassign it to the arrPassword array like;

arrPassword = arrPassword.slice(-passarrayLength);

like

[1,2,3].slice(-5);             // <- [1,2,3]
[1,2,3,4,5,6,7,8,9].slice(-5); // <- [5,6,7,8,9]

3 Comments

You miss the point, the question was if something like that exists. So a slice(-X) would mean it will always limit the array to x no matter what the length is ? I wrote a function which checks before push the length and then slices 1 if its 20. slice(-X) seems to be a cleaner way if there is no drawbacks
@ALP The problem is... You can not have such a generic functionality because it can not tell from where you insert the new item and what you want to do accordingly. i.e.you can use .unshift(), .splice(), .push() or even a[a.length] = x to insert a new element. It's very undeterministic so the implementation of such a functionality is left to the programmer. In this particular case it seems logical to .push() a new item to your array whenever it happens and apply a .slice(-n) everytime before you need the resulting array.
Thanks for explanation

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.