简体   繁体   English

使用拆分,拼接来获取数组中玩家的姓氏

[英]Grabbing the last names of players in array using split, splice

Objective 目的

I'm trying to get just the last names of the players in a series of spans and then be able to store those lastNames in their own variable var lastNameA , var lastNameB ... 我正在尝试只获取一系列范围内的玩家的姓氏,然后将这些lastName存储在自己的变量var lastNameAvar lastNameB ...

Here is an example of some of the names, Pierre Turgeon , Larry Patey , Kelly Shattenkirk , Barret Jackman , Alex Pietrangelo , Jacques Plante and in one special case, the person's last name is St. Marseille 这是一些名字的示例,例如Pierre TurgeonLarry PateyKelly ShattenkirkBarret JackmanAlex PietrangeloJacques Plante在一个特殊情况下,此人的姓氏为St. Marseille

scripts.js scripts.js

In many cases, I will want the even number items in the array, but with St. Marseille, I want both the St. and Marseille 在许多情况下,我需要数组中的偶数项,但是对于圣马赛,我希望St. Marseille

    // Grabs the names of all the players in the span
    // Then, it joins the names together with a comma and a space
    var fullNames = $("li span").map(function(){
        return $(this).text();
    }).get().join(", ");

    // This breaks a name into their first name and last name
    fullNames.split(" ");

Well, you can split only at the first space, but then you'll run into trouble with names like John Steven Murphy, where the last name is just "Murphy." 好吧,您只能在第一个空格处分开,但随后您会遇到诸如John Steven Murphy之类的名字的麻烦,其中的姓氏只是“ Murphy”。 Then there are names like Gerard van der Sanden or Angelina de la Vega and such, where there is not just one space but two in the family name. 还有像杰拉德·范德·桑登(Gerard van der Sanden)或安吉丽娜·德拉维加(Angelina de la Vega)之类的名字,那里的姓氏不仅有一个空格,而且还有两个。

Bottom-line, it's always going to be wrong for certain inputs if you don't have a clear delimiter. 底线是,如果您没有明确的定界符,则对于某些输入总是错误的。

But if you work on the basis that the "first" name won't have any spaces (which is a flawed assumption): 但是,如果您基于“名字”将没有任何空格的工作(这是有缺陷的假设):

var lastNames = $("li.span").map(function() {
    var name = $(this).text();
    var index = name.indexOf(" ");
    return index == -1 ? name : name.substring(index + 1);
}).get();

 var lastNames = $("li.span").map(function() { var name = $(this).text(); var index = name.indexOf(" "); return index == -1 ? name : name.substring(index + 1); }).get(); console.log(lastNames); console.log("Note that it gets John Steven Murphy wrong, see answer text for details."); 
 <ul> <li class="span">Pierre Turgeon</li> <li class="span">Larry Patey</li> <li class="span">Kelly Shattenkirk</li> <li class="span">Barret Jackman</li> <li class="span">Alex Pietrangelo</li> <li class="span">Jacques Plante</li> <li class="span">Michel St. Marseille</li> <li class="span">Gerard van der Sanden</li> <li class="span">Angelina de la Vega</li> <li class="span">John Steven Murphy</li> </ul> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Using ES 6 destructuring: 使用ES 6解构:

let [first, middle, end] = str.split(' ');
if (!end) {
  end = middle;
  middle = '';
}

As noted in the comments, this is fragile. 如评论中所述,这是脆弱的。 Any answer you get here is going to be fragile . 您在这里得到的任何答案都将是脆弱的 You will have to tread carefully in any situation involving names, but for simple inputs this should work ok. 在涉及名称的任何情况下,您都必须谨慎行事,但是对于简单的输入,这应该可以。

I think threw a map function you just need tonsplit slice and join like this : 我认为抛出一个map函数,您只需要tonsplit slice并像这样加入:

 var persons = ["Pierre Turgeon", "Larry Patey", "Kelly Shattenkirk", "Barret Jackman", "Alex Pietrangelo", "Jacques Plante", "Thierry St Marseille"];
 var names = persons.map(person => person.split(" ").slice(1).join(" "));
 console.log(names);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM