how to copy elements from one array to another array in javascript?

Viewed 533

I have function which randomly selects values from arrayVariable and assingn them to paragraph element p[i].

I want to copy the randomly selected values to another array b[i].

How do i do it?

function rvalue() {
  var arrayVariable = ['1', '2', '3', '4', '5']
  arrayLength = arrayVariable.length;

  ptags = document.getElementsByName("values");
  for (i = 0; i < ptags.length; i++) {
    ptags[i].innerHTML = arrayVariable[Math.floor(Math.random() * arrayLength)];

  }
}
p {
  font-size: 2xpx;
  color: #000000;
 
}
<body onload="rvalue()">
<p name="values"></p>
</div>
<p name="values"></p>
</div>
<p name="values"></p>
</div>

</div>

5 Answers

To insert text into an HTML element it is better to use textContent instead of innerHTML.

Create an array to push the textContent in each iteration:

function rvalue() {
  var array = [];
  var arrayVariable = ['1', '2', '3', '4', '5']
  arrayLength = arrayVariable.length;

  ptags = document.getElementsByName("values");
  for (i = 0; i < ptags.length; i++) {
    ptags[i].textContent = arrayVariable[Math.floor(Math.random() * arrayLength)];
    array.push(ptags[i].textContent);
  }
  console.log(array);
}
p {
  font-size: 20px;
  color: #000000;
}
<body onload="rvalue()">

  <p name="values"></p>
  <p name="values"></p>
  <p name="values"></p>

</body>

Please Note: Your HTML has no opening div element and font-size has invalid value (2xpx).

It will store value in tempArray by index, you can use Array.push for normal insert in replace of tempArray[index] = arrayVariable[index];

tempArray.push(arrayVariable[index]);

and if you may get duplicate value in array if you use .push method then just use this code to remove duplicate entry

tempArray= tempArray.filter(function(val, i){
  return tempArray.indexOf(val)== i; 
});

function rvalue() {
    var arrayVariable = ['1', '2', '3', '4', '5']
    arrayLength = arrayVariable.length;
    var tempArray = [];

    ptags = document.getElementsByName("values");
    for (i = 0; i < ptags.length; i++) {
        var index =
            Math.floor(Math.random() * arrayLength);
        ptags[i].innerHTML = arrayVariable[index];
        tempArray[index] = arrayVariable[index];

    }
    console.log(tempArray);
}
p {
  font-size: 2xpx;
  color: #000000;
 
}
<body onload="rvalue()">
<p name="values"></p>
</div>
<p name="values"></p>
</div>
<p name="values"></p>
</div>

</div>

Like this

function randomizeParagraphs() {
    var possibleValues = ['1', '2', '3', '4', '5'],
        createdValues = [], // Empty array to store created values.
        pTags = document.getElementsByName("values");

    for (var i = 0; i < pTags.length; i++) {
        var item = possibleValues[Math.floor(Math.random() * possibleValues.length)];
        ptags[i].innerHTML = item;
        createdValues.push(item);
    }

    // Log the created values.
    console.log(createdValues);
}

Hey you can assign get values like this,

function rvalue() {
  var arrayVariable = ['1', '2', '3', '4', '5']
  arrayLength = arrayVariable.length;

  arrRandomGeneratedValue = [];
  ptags = document.getElementsByName("values");
  for (i = 0; i < 5; i++) {
    randomGeneratedValue = arrayVariable[Math.floor(Math.random() * arrayLength)];
    ptags[i].innerHTML = randomGeneratedValue;
    arrRandomGeneratedValue.push(randomGeneratedValue);

  }
 return arrRandomGeneratedValue;
}

rvalue();

well I dunno if I understood but if you want to save the random values use the push method in the new array variable that you want to use as storage.

  function rvalue() {
  var b = [];
 var arrayVariable = ['1', '2', '3', '4', '5']
 arrayLength = arrayVariable.length;

   ptags = document.getElementsByName("values");
   for (i = 0; i < ptags.length; i++) {
   ptags[i].innerHTML = arrayVariable[Math.floor(Math.random() * arrayLength)];
   b.push(ptags[i].innerHTML);  
 }
 }

I hope that is what you were looking for :P regards!

Related