Pass an array of objects to another function onclick

I just want to ask how to pass an array of objects to another function. I have a function

 function btnB(weekly) {
    console.log(weekly);
 }

 function btnA() { 
    const array = [{ abc : 123 }, { def : 456 }]        
    div.innerHTML = `<div onclick="btnB(${array[0]});"`;
    divList.appendChild(div);        
 }

 btnA();

And I’m getting an error

Uncaught SyntaxError: Unexpected identifier

Answer

You can’t substitute an object like that in a template literal. It converts the object to a string, which returns [Object object].

Use JSON.stringify() to convert it to an object literal.

function btnA() { 
    const array = [{ abc : 123 }, { def : 456 }]        
    div.innerHTML = `<div onclick='btnB(${JSON.stringify(array[0])});'`;
    divList.appendChild(div);        
 }

You also should use single quotes around the onclick value, because JSON uses double quotes around the object keys.