我想拆分数组的字符串,然后分配数字,最后返回它们

时间:2014-02-22 06:09:30

标签: javascript arrays string function split

我想拆分数组的字符串,重新排列,所以最后一个名字先跟着逗号,最后返回它们。例如,斯蒂芬库里将被归还为“库里,斯蒂芬。”

我很迷茫。我现在已经尝试过很多次了。 :(

function callPlayerLastNameFirst(){

var warriorsArray = new Array(5);
warriorsArray[0] = "Stephen Curry";
warriorsArray[1] = "Andre Iguodala";
warriorsArray[2] = "Klay Thompson";
warriorsArray[3] = "Andrew Bogut";
warriorsArray[4] = "David Lee";

var callPlayerLastNameFirst = names.split(warriorsArray[0]);
}

function replacer(){
return [warriorsArray[0]].join(' , ');
};

2 个答案:

答案 0 :(得分:2)

尝试链接分割,反转和加入。

var warriorsArray = new Array(5);
    warriorsArray[0] = "Stephen Curry";
    warriorsArray[1] = "Andre Iguodala";
    warriorsArray[2] = "Klay Thompson";
    warriorsArray[3] = "Andrew Bogut";
    warriorsArray[4] = "David Lee";

for (var i = 0; i < warriorsArray.length; i++)
    warriorsArray[i] = warriorsArray[i].split(' ').reverse().join(', ');

console.log(warriorsArray);
//["Curry, Stephen", "Iguodala, Andre", "Thompson, Klay", "Bogut, Andrew", "Lee, David"] 

答案 1 :(得分:0)

function callPlayerLastNameFirst(){

var warriorsArray = new Array(5);
warriorsArray[0] = "Stephen Curry";
warriorsArray[1] = "Andre Iguodala";
warriorsArray[2] = "Klay Thompson";
warriorsArray[3] = "Andrew Bogut";
warriorsArray[4] = "David Lee";

replace(warriorsArray)
console.log(warriorsArray)
}

function replace(arr){
    for(var i=0;i<arr.length;i++){
        var words=arr[i].split(" ");
        words.reverse();
       arr[i]=words.join(", ");
   }
};
callPlayerLastNameFirst();