skip to Main Content

For example

const array1 = [Male, Female];

const array2 = [18-20];

const array3 = [Self Employed, Part Time, Full Time];

output will be

First Array:- [Male, 18-20, Self Employed];

Second Array:- [Male, 18-20, Part Time];

Third Array:- [Male, 18-20, Full Time];

Fourth Array:- [Female, 18-20, Self Employed];

Fifth Array:- [Female, 18-20, Part Time];

Sixeth Array:- [Female, 18-20, Full Time];

3

Answers


  1. We can do this by getting random item from each array and adding them to a new array. we are using the pick function get a random item each item.

    then we are using the for loop to create the final arrays.

    const pick = function(items) {
      return items[Math.floor(Math.random() * items.length)];
    };
    
    const array1 = ["Male", "Female"];
    const array2 = [18,19,20];
    const array3 = ["Self Employed", "Part Time", "Full Time"];
    
    for (let index = 0; index < 10; index++) {
      var created_array = [];
      created_array.push(pick(array1));
      created_array.push(pick(array2));
      created_array.push(pick(array3));
      
      console.log(created_array);
    }
    Login or Signup to reply.
  2. To pick one item from each of three arrays and create a new array in JavaScript, you can use a combination of array manipulation methods like Math.random() and push(). Here’s an example of how you can achieve this:

    for know more visit my website

    https://askmequora.com/posts/we-have-3-array-and-how-pick-1-item-from-each-array-and-push-a-new-array-using-javascript

    Login or Signup to reply.
  3. I can use foreach loop and array by array store the answer in output array

    const array1 = ["Male", "Female"];
            const array2 = ["18-20"];
            const array3 = ["Self Employed", "Part Time", "Full Time"];
            let output=[];
            array1.forEach((item1) => {
                array3.forEach((item2) => {
                    array2.forEach((item3) => {
                        output.push([item1,item3,item2]);
                    })
                })
            });
            console.log(output);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search