-1

i have 2 arrays with both database rows in them, but i want to loop through both of them randomly

let's say these are my arrays :

var fruit = [ "apple", "banana", "strawberry", "coconut"];
var cars = [ "mercedes", "bmw", "audi", "ferrari"];

then my output should be :

apple banana mercedes strawberry bmw audi coconut ferrari
  • The JSON part of the above is completely irrelevant to your question, suggest removing it. You have arrays, as you said. That's all we need to know. – T.J. Crowder Apr 29 '17 at 07:24

1 Answers1

0

Simply create a new fruit/car array:

var arr=fruit.concat(cars);
var result=[];

Then loop and take a random el out of the array and add it to our result:

for(i=arr.length;i>0;i--){
  result.push(arr.splice(Math.round(Math.random()*(arr.length-1)),1));
}

Then create a string and display it:

result=result.join(" ");
console.log(result);

http://jsbin.com/qasomoluke/edit?console

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151