如何从列表中删除@ yahoo.com并对其进行排序?

时间:2015-08-28 18:20:05

标签: javascript

<p id="demo"></p>

<script>

//This is the email list
var emailList =["adam@yahoo.edu\n", "henry@yahoo.edu\n", "john@yahoo.edu\n", "sally@yahoo.edu\n", "adam@yahoo.edu\n", "david@yahoo.edu\n", "myhome@yahoo.edu\n", "david@yahoo.edu\n", "david@yahoo.edu\n", "hunger@yahoo.edu\n", "madison@yahoo.edu\n", ];

document.getElementById("demo").innerHTML = emailList;

//I want to remove the @yahoo.com and then sort the list
function myFunction() {
   var myEmailList = emailList.replace("@yahoo.com", " ");
   myEmailList.sort();
   document.getElementById("demo").innerHTML = myEmailList;
}

</script>

</body>
</html>

7 个答案:

答案 0 :(得分:2)

一个单行,人们喜欢单行

var names = emailList.map(function(e){ return e.split('@').shift(); }).sort();

如果您想专门针对@yahoo*,请将拆分参数更改为&#39; @ yahoo&#39;

 var names = emailList.map(function(e){ return e.split('@yahoo').shift(); }).sort();

这将使非雅虎地址保持不变。

答案 1 :(得分:1)

您可以使用地图功能将当前列表转换为没有yahoo部分的列表。

var myEmailList = emailList.map(function(email){
    return email.replace("@yahoo.com", "");
});

然后你可以对它进行排序。

myEmailList.sort();

您也可以考虑加入列表,以便在将其放回html之前看起来很漂亮。

myEmailList.join(","); // joins the elements with a , in between

答案 2 :(得分:1)

您需要循环遍历数组中的每个项目并替换字符串:

function myFunction() {
    for (var i = 0; i < emailList.length; i++) {
        emailList[i] = emailList[i].replace("@yahoo.com", "");
    }
    emailList.sort();
}

不要忘记在需要时调用该功能:

myFunction();

如果要将数组中的每个项目附加到div标记中的DOM元素:

function appendToDemo() {
     emailList.map(function(email) {
        document.getElemendById("demo").append(<div>email</div>);
    }
}

(或其他类似的效果)

答案 3 :(得分:0)

您可以使用for循环:

for (var i in myEmailList) {
    myEmailList[i] = myEmailList[i].replace("@yahoo.com");
}

或地图功能:

myEmailList.map(function(email){return email.replace("@yahoo.com", "");})

清除列表中字符串中每次出现的“@ yahoo.com”。

然后,对列表进行排序:

myEmailList.sort();

答案 4 :(得分:0)

尝试以下代码

Array.prototype.remove = function(replaceString){
 this.join('#').replace(replaceString,'').split('#').sort(); }
emailList.remove('@yahoo.com');

答案 5 :(得分:0)

使用数组方法:

像这样:

&#13;
&#13;
var emailList = [ "adam@yahoo.edu\n", "henry@yahoo.edu\n", "john@yahoo.edu\n",
                  "sally@yahoo.edu\n", "adam@yahoo.edu\n", "david@yahoo.edu\n",
                  "myhome@yahoo.edu\n", "david@yahoo.edu\n","david@yahoo.edu\n",
                  "hunger@yahoo.edu\n", "madison@yahoo.edu\n" ];

emailList = emailList
         .map(function(address){ return address.replace(/@yahoo\.edu\n/,''); })
         .sort(function(a, b){ return a < b ? -1 : a > b ? 1 : 0; })
         .filter(function(address, index, array){ return array.indexOf(address) == index; });

document.write(JSON.stringify(emailList));

// ===  ["adam", "david", "henry", "hunger", "john", "madison", "myhome", "sally"]
&#13;
&#13;
&#13;

答案 6 :(得分:0)

这里只是一个简短的解决方案,使用拼接功能,然后对其进行排序:

var emailList =["adam@yahoo.edu\n", "henry@yahoo.edu\n", "john@yahoo.edu\n", "sally@yahoo.edu\n", "adam@yahoo.edu\n", "david@yahoo.edu\n", "myhome@yahoo.edu\n", "david@yahoo.edu\n", "david@yahoo.edu\n", "hunger@yahoo.edu\n", "madison@yahoo.edu\n" ];

     for(var count = 0;count <= emailList.length; count++) {
        if(emailList[count] === "hunger@yahoo.edu\n" ) {
        emailList.splice(count, 1);
        break;
        }
        };

     emailList.sort();