本文介绍了Javascript/Jquery将字符串转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串

var traingIds = "${triningIdArray}";  // ${triningIdArray} this value getting from server
alert(traingIds)  // alerts [1,2]
var type = typeof(traingIds )
alert(type)   // // alerts String

现在我想将其转换为数组,以便可以迭代

now i want to convert this to array so that i can iterate

我尝试过

var trainindIdArray = traingIds.split(',');
$.each(trainindIdArray, function(index, value) {
    alert(index + ': ' + value);   // alerts 0:[1 ,  and  1:2]
});

如何解决这个问题?

推荐答案

假设情况似乎是这样,${triningIdArray}是服务器端占位符,已替换为JS数组文字语法,只是失去了引号.所以:

Assuming, as seems to be the case, ${triningIdArray} is a server-side placeholder that is replaced with JS array-literal syntax, just lose the quotes. So:

var traingIds = ${triningIdArray};

不是

var traingIds = "${triningIdArray}";

这篇关于Javascript/Jquery将字符串转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 18:16