2

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;
}
3
  • what are Json-indexes? JSON is a string, representing serialized data. Commented Jan 18, 2017 at 9:56
  • I removed Json-mentioning from the question if it is confusing someone. Commented Jan 18, 2017 at 10:42
  • you may add the data as well. Commented Jan 18, 2017 at 10:44

2 Answers 2

1

You could use directly Set with size property for checking the wanted size of the result set.

var array = [0, 1, 42, 17, 22, 3, 7, 9, 15, 35, 20],
    unique = new Set;

while (unique.size < 4) {
    unique.add(Math.floor(Math.random() * array.length));
}

console.log([...unique]); // indices

Otherwise, you could use a hash table.

var array = [0, 1, 42, 17, 22, 3, 7, 9, 15, 35, 20],
    unique = {},
    id = [],
    i;

while (id.length < 4) {
    i = Math.floor(Math.random() * array.length);
    if (!unique[i]) {
        id.push(i);
        unique[i] = true;
    }
}

console.log(id);

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

4 Comments

I tried to use Set-object but it does not work, there are same values in it. .add works but .has doesn't. I strongly believe that Set is not fully supported in React-Native.
Size does work. But it returns always 4 even if the values are not unique.
if the first does not work for you, yould use the second one, with a hash table.
Your second example seems to work in my code. Thanks.
1

As I understood your problem, you want to get N random values from array, values should be unique. You've just shuffle you array and get first N values from it.

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.