简体   繁体   中英

How to convert json object to array in javascript

I need to create an array from JSON data in node.js. I dont want to use jquery selector for this.

data = { List : ['1' , '2' , '3'] }

I am sending this data in the ajax call (POST). At the server end receving is:-

reqArray = req.param('List');
reqArray contains:- ['1' ,'2' ,'3']

I need this reqArray as an input to $in in mongoDb ,where it takes array as as input.

In the format [1 ,2 , 3] please suggest a way of doing this.

Try using the map function:

var numberArray = reqArray.map(function(element) {
    return +element;
});

The + will automatically convert it to a number.

Like correctly commented it is even better to use:

var numberArray = reqArray.map(Number);

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