简体   繁体   中英

How do i access an array object inside a function in ejs

I have an array object passed to my ejs file (front end) from NodeJS backend code. I need to access this array object inside a function which is called on click of a button in my ejs file. Could you please let me know how do I access the array object inside the function.

I am passing the array object data from NodeJS (in app.js) to my ejs file (gallery.ejs) like this:

res.render("gallery",{data:billdata})  

I am able to access the array object 'data' in gallery.ejs . For example i can access an element of array object 'data' in gallery.ejs like this:

<%data._embedded.tickets[0].totals.sub_total%>

Now I want 'data' to be accessed inside a function defined in gallery.ejs , the function is called on click of a button. I want to pass the 'data' object to the function myFunction below so that the object can be accessed from within the function. How do I do that?

<button type="button" id="zero_perc" class="btn btn-outline-primary" onclick="myFunction()">0%</button>

I am not sure how to pass the array object 'data' to the function myFunction() . Any pointers or code samples will be very helpful.

function myFunction() {
    for(var i in arguments) {
        console.log(arguments[i]); // U can access all objects here
    }
}
var params = [10, 15];
myFunction(...params);

// It could be called using array of objects
//var params = [{a:1}, {b:1}];
//myFunction(...params);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM