[英]How to convert json object to array in javascript
I need to create an array from JSON data in node.js. 我需要从node.js中的JSON数据创建一个数组。 I dont want to use jquery selector for this.
我不想为此使用jquery选择器。
data = { List : ['1' , '2' , '3'] }
I am sending this data in the ajax call (POST). 我正在ajax调用(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. 我需要这个
reqArray
作为一种投入, $in
在mongoDb
,它需要数组作为输入。
In the format [1 ,2 , 3]
please suggest a way of doing this. 请以
[1 ,2 , 3]
格式提出一种解决方法。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.