使用两个不同的定界符将数组中的项目连接起来 [英] Join items in an array with two different delimiters

查看:45
本文介绍了使用两个不同的定界符将数组中的项目连接起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决jshero.net上的一个挑战.挑战是:

I am trying to solve a challenge on jshero.net. The challenge is :

编写一个函数列表,该函数列表包含一个单词数组,并通过将数组中的单词连接起来并返回字符串,字符串之间用逗号分隔,最后一个单词用与"分隔.空数组应返回一个空字符串.

Write a function list that takes an array of words and returns a string by concatenating the words in the array, separated by commas and - the last word - by an 'and'. An empty array should return an empty string.

示例: list([''Huey','Dewey','Louie'])应该返回'Huey,Dewey and Louie'

我能想到的最佳解决方案是:

Best solution I could come up with was this :

function list (a){   
  let myList = a.join(' and ' )
  return myList
}

问题是我无法弄清楚如何使函数仅在第二个名字之后而不在第二个名字之前返回和"字.有谁知道如何解决这个问题?........................................................

The problem is that I cannot figure out how to make the function to return just the "and" word just after the second name and not before it. Does anyone know how to solve this? ............................................................

更新:我找到了另一种选择:

Update : I found an alternative :

function list(arr){
    let myL= arr.slice(0,2);
    let myLi= myL.join(' , ')
    let myL2 = arr.slice(2,3);
    let myLi2= myL2.join(' and ');
    let myList = myLi + myLi2 
    if (arr.length <=2){
        return arr.join(' and ')} else {
            return myList}
} 

我很接近,结果是"Huey,DeweyLouie" ,但不知何故仍未在其中添加"add"一词.有人有想法吗?

I am close, the outcome is "Huey , DeweyLouie" but somehow is still not adding the "add" word into it. Anyone got an ideea?

推荐答案

有趣的用例.实际上,我认为这将是对我标记的模板库的一个很好的补充!

Interesting use case. In fact I thought it would be a nice addition to my tagged templates library!

所以我们有两个分隔符:

So we have two separators:

  • delim:','
  • delimlast:'和'

还有一些情况:

  • [] -> ''
  • ['Huey'] -> 'Huey'
  • ['Huey','Dewey'] -> 'Huey and Dewey'
  • ['Huey','Dewey','Louie'] -> 'Huey,Dewey and Louie'
  • [] -> ''
  • ['Huey'] -> 'Huey'
  • ['Huey', 'Dewey'] -> 'Huey and Dewey'
  • ['Huey', 'Dewey', 'Louie'] -> 'Huey, Dewey and Louie'

这是解决此问题的一种方法:

Here's one way to approach this:

const list =
  (xs, delim = ', ', delimlast = ' and ') =>
      !Array.isArray(xs) ? xs
    : xs.length === 0    ? ''
    : xs.length === 1    ? xs[0]
    : xs.length === 2    ? xs[0] + delimlast + xs[1]
                         : xs.slice(0, -1).join(delim) + delimlast + xs[xs.length-1];


list('Huey');                     // 'Huey'
list([]);                         // ''
list(['Huey']);                   // 'Huey'
list(['Huey', 'Dewey']);          // 'Huey and Dewey'
list(['Huey', 'Dewey', 'Louie']); // 'Huey, Dewey and Louie'


我是一个带标签的小模板库的作者(还有另一个!),您的问题让我想到了另一个.谢谢!

import {list} from '<DETAILS CAN BE FOUND IN MY PROFILE IF YOU ARE INTERESTED>';

const guys = ['Huey', 'Dewey', 'Louie'];
list`Hey ${guys}!`;
//=> "Hey Huey, Dewey and Louie!"

这篇关于使用两个不同的定界符将数组中的项目连接起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆