I am making a feature where random indexes are added to an array with for-loop, values must be always different but somehow there are sometimes (approximately every 10. call of function) same values even I am trying to filter same values from the answerId-array and put rest of values unique-array and check are filtered unique-array and original answerId-array having same length, if unique-arrays length is lower than unique-arrays length, answerIds values will be changed. But these arrays have always same length even if there are same values in answerId-array and running cannot pass to rest of for-loops (those for-loops are quite some hack code). What I am doing wrong? Sorry if my english skills are not good.
getJSONData(){
var answerId = [null, null, null, null];
var length = Object.keys(Company.person).length;
for(var i = 0; i <= answerId.length - 1; i++){
answerId[i] = [Math.floor(Math.random() * length)]
}
let unique = Array.from(new Set(answerId))
console.log(unique.length)
if (unique.length < answerId.length){
for(var i = 0; i <= answerId.length - 1; i++){
answerId[i] = [Math.floor(Math.random() * length)]
}
console.log("new values 1")
unique = Array.from(new Set(answerId))
if (unique.length < answerId.length){
for(var i = 0; i <= answerId.length - 1; i++){
answerId[i] = [Math.floor(Math.random() * length)]
}
console.log("new values 2")
unique = Array.from(new Set(answerId))
if (unique.length < answerId.length){
for(var i = 0; i <= answerId.length - 1; i++){
answerId[i] = [Math.floor(Math.random() * length)]
}
console.log("new values 3")
unique = Array.from(new Set(answerId))
if (unique.length < answerId.length){
for(var i = 0; i <= answerId.length - 1; i++){
answerId[i] = [Math.floor(Math.random() * length)]
}
console.log("new values 4")
}
}
}
}
var personArray = [Company.person[answerId[0]].firstName + ' ' + Company.person[answerId[0]].lastName, Company.person[answerId[1]].firstName + ' ' + Company.person[answerId[1]].lastName, Company.person[answerId[2]].firstName + ' ' + Company.person[answerId[2]].lastName, Company.person[answerId[3]].firstName + ' ' + Company.person[answerId[3]].lastName];
return personArray;
}
Json-indexes? JSON is a string, representing serialized data.