Get first letter of each word in a string, in JavaScript

后端 未结 17 1634
后悔当初
后悔当初 2020-12-05 04:00

How would you go around to collect the first letter of each word in a string, as in to receive an abbreviation?

Input: "Java Script Object

17条回答
  •  [愿得一人]
    2020-12-05 04:23

    To add to the great examples, you could do it like this in ES6

    const x = "Java Script Object Notation".split(' ').map(x => x[0]).join('');
    console.log(x);  // JSON
    

    and this works too but please ignore it, I went a bit nuts here :-)

    const [j,s,o,n] = "Java Script Object Notation".split(' ').map(x => x[0]);
    console.log(`${j}${s}${o}${n}`);
    

提交回复
热议问题