Javascript 使用jquery拆分数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10257605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:31:10  来源:igfitidea点击:

array split using jquery?

javascriptjqueryhtml

提问by Carlos

i have some value stored in array and i wana split them and wana know length of its contains value but when i am running function it is not working

我有一些值存储在数组中,我想拆分它们,并且知道其包含值的长度,但是当我运行函数时,它不起作用

<head>

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">

$(function(){


    var valData= ['songs','video','movie','games','other'];

    var valNew=valData.split(',');

    for(i=0;i<valNew.length;i++);

    alert(valNew.length)

    })


</script>


</head>

<body>

<select id="me"></select>
</body>

回答by Kevin Bowersox

Split is used to separate a delimited string into an array based upon some delimiter passed into the split function. Your values are already split into an array. Also your for loop syntax is incorrect.

Split 用于根据传递给 split 函数的某些分隔符将分隔的字符串分成数组。您的值已拆分为一个数组。您的 for 循环语法也不正确。

Split Documentation: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split

拆分文档:https: //developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split

Corrected code:

更正的代码:

$(function(){
    var valData= "songs,video,movie,games,other";

    var valNew=valData.split(',');

    for(var i=0;i<valNew.length;i++){
        alert(valNew.length)
    }
});

http://jsfiddle.net/tmHea/

http://jsfiddle.net/tmHea/

回答by elclanrs

You don't need to split anything, it's already an array. And your for loop syntax is wrong...

你不需要拆分任何东西,它已经是一个数组。而你的 for 循环语法是错误的...

for (var i = 0; i < valData.length; i++) {

    alert(valData[i].length);

}

回答by Tats_innit

Hiya demohttp://jsfiddle.net/YCarA/8/

Hiya演示http://jsfiddle.net/YCarA/8/

2 things are wrong

2件事是错误的

1) for loop.

1) for 循环。

2) your valDatais array it should be string for split to format as array.

2)你valData是数组它应该是字符串拆分为数组格式。

jquery code

查询代码

$(function(){


    var valData = "songs,video,movie,games,other";


    var valNew = valData.split(',');

    for(var i=0;i<valNew.length;i++){

      alert(valNew.length);

   }
    });?

回答by Shaikh Farooque

Kmb385 is right. Your data is you can not split to array it is already separated.

Kmb385 是对的。您的数据是您无法拆分为已分离的数组。

Also your for loop is faulty the correct one is

您的 for 循环也有问题,正确的是

for(var i=0;i<valNew.length;i++)
      alert(valNew[i].length);

回答by tusar

The jQuery.each()way :

jQuery.each()方法:

var valData = ["songs","video","movie","games","other"];
$.each(valData, function(key, value) {
   alert("Index ---> " + key + ' & length of item ---> ' + value.length);
});   

jsfiddle

提琴手