Reputation: 3
I have constant elements array named items and want to copy it to arr2.
How to copy constantant elements array to another array?
I tried to use splice
but it didn't work.
window.onload="rvalue()"
var tempimages = [];
var arr2=[];
function rvalue()
{
const items = [
{ label: '1', url: '1.jpg' },
{ label: '2', url: '2.jpg' },
{ label: '3', url: '3.jpg' },
{ label: '4', url: '4.jpg' },
{ label: '5', url: '5.jpg' },
{ label: '6', url: '6.jpg' },
{ label: '7', url: '7.jpg' },
{ label: '8', url: '8.jpg' },
{ label: '9', url: '9.jpg' },
{ label: '10', url: '10.jpg'},
{ label: '11', url: '11.jpg'},
{ label: '12', url: '12.jpg'},
]
ptags = document.querySelectorAll('[name="values"]');
arr2 = items.splice(0);
for (let index = 0; index < 4;index++)
{
randomIndex = Math.floor(Math.random() * items.length),var item2[index]=['item.label','item'];
var item2[index]=['item.label','item'];
item = items[randomIndex];
ptags[index].textContent = item.label;
tempimages.push(item);
}
console.log(item2);
}
Upvotes: 0
Views: 54
Reputation: 722
If you want to just copy them you can do:
const arr2 = [...items]
https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Operators/Spread_syntax
Upvotes: 1