简体   繁体   English

如何将逗号分隔值设置为 javascript 中的数组?

[英]How to set comma separate values to array in javascript?

method1('id','name')

This is the method I called.这是我调用的方法。

Array.prototype.method1 = function (property) {

  console.log(property)  //Expect ['id','name']
)

I want to get result like ['id','name']我想得到像 ['id','name'] 这样的结果

const property = property.split(",");
console.log(property)    //This shows only ['id']

Can anyone help me to do this?谁能帮我做这个?

Here you can try two ways:在这里你可以尝试两种方式:

 function check() { console.log([...arguments]) } check(1,2,"apple")

Try the following.尝试以下操作。 Will output what you're expecting, but not sure that JS experts would write code like this. output 会是您所期望的,但不确定 JS 专家是否会编写这样的代码。

var s = "id,name";
var a =  s.split(",");

Array.prototype.method1 = function(property) {
  console.log(property);  //Expect ['id','name']
}
a.method1(a);    //this will display your expected

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

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