0

I have array with multiple objects, for instance,

var arr = ["a", "b", "c", "d"];

What I want is to pick up 2 random objects from that array, for instance "a" and "c", and push the 2 objects into another array, for instance "arrCopy". Another thing is, I want to have a copy of the elements in the array "arrCopy", which are also shuffled. So, that array should look like:

arrCopy = ["c", "a", "a", "c"];

Last thing, how can I compare them if their content is the same? For instance:

arrCopy[1] == arrCopy[2];
1

4 Answers 4

1

For your first question:

var index1 = parseInt(Math.random() * arr.length, 10);
var index2 = index1;
while(index1 == index2) {
  index2 = parseInt(Math.random() * arr.length, 10)
}

var arrCopy = [arr[index1], arr[index2]]

Can you clarify the second part of your question, it doesn't sound like 'Another thing is, I want to have a copy of the elements in the array "arrCopy"' matches up with your example since your example has 4 elements instead of the 2 you picked from the array.

Edit:

For your second question replace:

var arrCopy = [arr[index1], arr[index2]]

with:

var arrCopy = [arr[index1], arr[index2], arr[index1], arr[index2]]

For your third Question:

arrCopy1 === arrCopy2 would be the shallow comparison (are they the same address in memory)

var areEqual = true;

for (var i = 0; i < arrCopy1.length; ++i) {
    if (arrCopy1 [i] !== arrCopy2[i]) {
        areEqual = false;
        break;
    }
}

then check areEual for deep equality (whether array content are the same).

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

1 Comment

well, it means this: get 2 random elements [a, c], push them to another array, and also make a copy of them, so the new array would have 4 elements [a, c, a, c]
0

Use any randomization algorithm(For eg: fisher yates algorithm)..http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

Randomize the objects in the array and

Pick any two elements like

var firstElement = arr[Math.floor(Math.random()*arr.length)]
var secElement = arr[Math.floor(Math.random()*arr.length)]

Comments

0

Found a simplest way from here..Sort helps to do this in a shorter way

[1,2,3,4,5,6].sort(function() {
  return .5 - Math.random();
});

1 Comment

Note as well that this returns a randomized array -- not a single value (or two as OP requests).
0

This should do the trick:

function shuffle(array) {
  var currentIndex = array.length
    , temporaryValue
    , randomIndex
    ;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

var arr = [{a:{count:3}}, {b:{count:2}}, {c:{count:3}}, {d:{count:4}}];

var num1,num2;
var shuffled_arr=shuffle(arr);
item1=arr[0];
item2=arr[1];

var arrCopy=shuffle([item1,item2,item1,item2]);

for(var i=0;i<arrCopy.length;i++){
    for(var g=i+1;g<arrCopy.length;g++){
        if(JSON.stringify(arrCopy[i])==JSON.stringify(arrCopy[g])){
            console.log('item in index ' + i +' is equal to item in index '+ g)
            console.log(arrCopy[i], arrCopy[g])
        }
    }    
}

2 Comments

this look fine, but how can i compare the value of the 2 items?
@user3652674 are the 2 items numbers, string, arrays, objects or what?

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.