I am building an online language experiment using JavaScript. As a beginner-level javaScript learner, I am having difficulties shuffling a list based on specific requirements and sampling items from the shuffled list. I would very much appreciate any help in figuring out how to do this.
I have a list of words as follows. Condition X1 = X2, Y1 = Y2. This is to make things clearer later.
word_number | condition | word |
---|---|---|
w1 | X1 | a |
w1 | X2 | b |
w1 | Y1 | c |
w1 | Y2 | d |
w2 | X1 | e |
w2 | X2 | f |
w2 | Y1 | g |
w2 | Y2 | h |
w3 | X1 | i |
w3 | X2 | j |
w3 | Y1 | k |
w3 | Y2 | l |
w4 | X1 | m |
w4 | X2 | n |
w4 | Y1 | o |
w4 | Y2 | p |
First, I would like to shuffle the list so that the four words of the same word_number will stay together. I expect the list to be like this:
word_number | condition | word |
---|---|---|
w2 | X1 | e |
w2 | X2 | f |
w2 | Y1 | g |
w2 | Y2 | h |
w4 | X1 | m |
w4 | X2 | n |
w4 | Y1 | o |
w4 | Y2 | p |
w3 | X1 | i |
w3 | X2 | j |
w3 | Y1 | k |
w3 | Y2 | l |
w1 | X1 | a |
w1 | X2 | b |
w1 | Y1 | c |
w1 | Y2 | d |
Second, I would like to shuffle the two words of the same condition in the list. I expect the list to be like this.
word_number | condition | word |
---|---|---|
w2 | X2 | f |
w2 | X1 | e |
w2 | Y2 | h |
w2 | Y1 | g |
w4 | X1 | m |
w4 | X2 | n |
w4 | Y2 | p |
w4 | Y1 | o |
w3 | X2 | j |
w3 | X1 | i |
w3 | Y1 | k |
w3 | Y2 | l |
w1 | X1 | a |
w1 | X2 | b |
w1 | Y2 | d |
w1 | Y1 | c |
Lastly, I want to select one word from each word_number, including two from X condition and two from Y condition. So basically, select the first word of word_number 2 and 4 in X condition and the first word of word_number 3 and 1 in Y condition.
word_number | condition | word |
---|---|---|
w2 | X2 | f |
w4 | X1 | m |
w3 | Y1 | k |
w1 | Y2 | d |
This final list is what I would like to have for my experiment.