简体   繁体   English

在循环中使用扩展运算符与 javascript 中的 object 文字

[英]Using spread operator in a loop with object literals in javascript

I have generated arrays from an ajax response.我从 ajax 响应中生成了 arrays。 An example of an array is数组的一个例子是

var prds = ["248", "NORFLOX 200 MG", "22"]

I have checked the array by Array.isArray() method and the response is true.我已经通过Array.isArray()方法检查了数组,并且响应为真。

In the same loop i pass the above array as an argument in the onclick event function like so...在同一个循环中,我将上述数组作为参数传递给 onclick 事件 function 就像这样......

str += `<li onclick=loadProductEditForm(${prds})>${productsj[i]['medicine_name']}<li>';

When i use the function loadProductEditForm(array) it does not work and give me this error in the console....当我使用 function loadProductEditForm(array)它不起作用并在控制台中给我这个错误....

Uncaught SyntaxError: missing ) after argument list. Uncaught SyntaxError: missing ) 在参数列表之后。

I have tried this too...这个我也试过。。。

str += `<li onclick=loadProductEditForm(...${prds})>${productsj[i]['medicine_name']}<li>';

What am I doing wrong?我究竟做错了什么?

loadProductEditForm(${prds}) will expand to loadProductEditForm(${prds})将扩展为
loadProductEditForm("248,NORFLOX 200 MG,22") so perhaps you want loadProductEditForm("248,NORFLOX 200 MG,22")所以也许你想要
loadProductEditForm(prds) if your function needs an array loadProductEditForm(prds)如果您的 function 需要一个数组

 var prds = ["248", "NORFLOX 200 MG", "22"] console.log(`${prds}`) console.log(prds)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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