简体   繁体   中英

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']

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.

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

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