how to create an array with an array of divs?

Viewed 129

Please help to figure out. For example, i have 5 divs. I need to push it like one whole array into another array or obj, like this:

let array = [ [div , div , div, div, div], [more divs] ]

I'm trying to do that:

array[index][index].push(div)

But it's replace the previous content.. it's logical behavior I just want this array to be a placeholder for all of divs to simply show the necessary content on click Also i understand that it's a data structures that i'm bad in. If you have an advice how to better store it, i will be appreciate Thank you in advance

3 Answers

If I got you correctly, array.unshift() solves the problem:

const arrayBase = ["older","content","here"];
const arrayToAdd = ["div","div","div","div","div"];
arrayBase.unshift(arrayToAdd);
console.log(arrayBase);

You can also use the spread operator to achieve the same effect:

const arr = ["div", "div", "div", "div", "div"]
const itemsToAdd = ["new", "content", "here"]

console.log([...arr, itemsToAdd])

Assuming you want to merge two arrays into a new array, here's how you can do it:

const firstArray = ['div', 'div', 'div', 'div'];
const secondArray = ['div2', 'div2']
const resultArray = firstArray.concat(secondArray)
Related