Is there a way to join the elements in an js array, but let the last separator be different?

前端 未结 11 784
灰色年华
灰色年华 2021-02-03 17:40

What I want is something like Array.join(separator), but which takes a second argument Array.join(separator, beforeLastElement), so when I say [f

11条回答
  •  太阳男子
    2021-02-03 18:08

    there is a package join-array

    const join = require('join-array');
    const names = ['Rachel','Taylor','Julia','Robert','Jasmine','Lily','Madison'];
    const config = {
      array: names,
      separator: ', ',
      last: ' and ',
      max: 4,
      maxMessage:(missed)=>`(${missed} more...)`
    };
    const list = join(config); //Rachel, Taylor, Julia, (3 more...) and Madison
    

提交回复
热议问题