需要删除数组中的连字符

时间:2016-06-27 11:57:13

标签: javascript jquery html5 css3

我有一个包含一些元素的数组。

var arr=["bet-vet","ban-van","baskat-vase"];

在显示数组时,想要删除单词之间的连字符。在显示时 用户应该看到

Bet,vet,ban,van,baskat,vase

Here is the link of my code

5 个答案:

答案 0 :(得分:3)

你可以尝试:

var arr = ["bet-vet", "ban-van", "baskat-vase"];

arr= arr.join("-").split("-").join();

console.log(arr);

答案 1 :(得分:0)

你可以试试这个

<script>
var b_or_v =["bet-vet","bat-vat","reb-rev","bick-vic","ban-van"];

function removeHyphen()
{
    var newArray = [];
    for(i=0;i<b_or_v.length;i++)
    {
        res = b_or_v[i].split("-");
        newArray.push(res[0]);
        newArray.push(res[1]);
    }
    console.log(newArray);
    document.getElementById("text").innerHTML = newArray;
}
</script>
<body>
    <button onclick="removeHyphen()">Find Val</button>
    <div id="text">
    </div>
</body>

这是jsFiddle link

答案 2 :(得分:0)

尝试使用如下所述的代码:

document.getElementById("btn-1").onclick= function(){
document.getElementById("submit").onclick= function(){
  var txt = [];
  for (var i = 0; i < b_or_v.length; i++) { 
    if(b_or_v[i].match(/-/g)){
      txt += b_or_v[i].split('-');
      txt += ', ';
    }
    else{
      txt += b_or_v[i]+', ';

    }
}

输出:b_or_v:下注,兽医,蝙蝠,增值税,反击,转速,比克,维克,禁令,范,

答案 3 :(得分:0)

使用Javascript Joint() & Split() method

示例:

var arr=["bet-vet","ban-van","baskat-vase"];
var arrJoin = arr.join("-");
var arrsplit = arrJoin.split("-");
console.log(arrsplit);

答案 4 :(得分:0)

这样的事情:

&#13;
&#13;
var arr = ["bet-vet", "ban-van", "baskat-vase"];
var arr1 = [];
for(var i= 0; i < arr.length;i++){
  test= arr[i].replace('-', '');
  arr1.push(test);
}
console.log(arr1.join(', '));
&#13;
&#13;
&#13;