随机姓名生成函数,毕竟懒得打字
发布于 8 年前 作者 nasaplayer 8560 次浏览 来自 分享

知道都会 ,毕竟是我大Node.js,没中级js基础 根本玩不下去~ function getRandomName(){ var firstNames = new Array( “赵”, “钱”, “孙”, “李”, “周”, “吴”, “郑”, “王”, “冯”, “陈”,
“褚”, “卫”, “蒋”, “沈”, “韩”, “杨”, “朱”, “秦”, “尤”, “许”, “何”, “吕”, “施”, “张”, “孔”, “曹”, “严”, “华”, “金”, “魏”,
“陶”, “姜”, “戚”, “谢”, “邹”, “喻”, “柏”, “水”, “窦”, “章”, “云”, “苏”, “潘”, “葛”, “奚”, “范”, “彭”, “郎”, “鲁”, “韦”,
“昌”, “马”, “苗”, “凤”, “花”, “方”, “俞”, “任”, “袁”, “柳”, “酆”, “鲍”, “史”, “唐”, “费”, “廉”, “岑”, “薛”, “雷”, “贺”,
“倪”, “汤”, “滕”, “殷”, “罗”, “毕”, “郝”, “邬”, “安”, “常”, “乐”, “于”, “时”, “傅”, “皮”, “卞”, “齐”, “康”, “伍”, “余”,
“元”, “卜”, “顾”, “孟”, “平”, “黄”, “和”, “穆”, “萧”, “尹”, “欧阳”, “慕容” ); var secondNames = new Array( “子璇”, “淼”, “国栋”, “夫子”, “瑞堂”, “甜”, “敏”, “尚”, “国贤”, “贺祥”, “晨涛”, “昊轩”, “易轩”, “益辰”, “益帆”, “益冉”, “瑾春”, “瑾昆”, “春齐”, “杨”, “文昊”, “东东”, “雄霖”, “浩晨”, “熙涵”, “溶溶”, “冰枫”, “欣欣”, “宜豪”, “欣慧”, “建政”, “美欣”, “淑慧”, “文轩”, “文杰”, “欣源”, “忠林”, “榕润”, “欣汝”, “慧嘉”, “新建”, “建林”, “亦菲”, “林”, “冰洁”, “佳欣”, “涵涵”, “禹辰”, “淳美”, “泽惠”, “伟洋”, “涵越”, “润丽”, “翔”, “淑华”, “晶莹”, “凌晶”, “苒溪”, “雨涵”, “嘉怡”, “佳毅”, “子辰”, “佳琪”, “紫轩”, “瑞辰”, “昕蕊”, “萌”, “明远”, “欣宜”, “泽远”, “欣怡”, “佳怡”, “佳惠”, “晨茜”, “晨璐”, “运昊”, “汝鑫”, “淑君”, “晶滢”, “润莎”, “榕汕”, “佳钰”, “佳玉”, “晓庆”, “一鸣”, “语晨”, “添池”, “添昊”, “雨泽”, “雅晗”, “雅涵”, “清妍”, “诗悦”, “嘉乐”, “晨涵”, “天赫”, “玥傲”, “佳昊”, “天昊”, “萌萌”, “若萌” );

    var firstLength = firstNames.length;
    var secondLength = secondNames.length;
    
    var i = parseInt(  Math.random() * firstLength );
    var j = parseInt(  Math.random() * secondLength );
    
    var name = firstNames[i]+secondNames[j];
    
    return name;
    
}//end getRandomName
15 回复

哇咔咔,node.js可以直接在回调函数传递对象!!!!! 貌似其他后端编程语言需要用JSON或者字符串转

const FIRST_NAMES = ...
const SECOND_NAMES = ...
let getRandomIndex = arr => arr.length * Math.random() | 0,
	getRandomName = arr => arr[getRandomIndex(arr)];

let getRandomName = () => [FIRST_NAMES, SECOND_NAMES].map(getRandomName).join('');

@Telanx 看不太懂啊大腿,我还是萌新

@nasaplayer 就是换成了箭头函数而已… 获取下标所在字符串,然后用 join 起来… parseInt 还是需要的… 话说你这个变量声明是不是应该提出去,否则,每次都声明这两个数组…

@DevinXian 没见过这种写法啊。ES5 ES6还是node.js特有的js写法

用闭包存一下数组声明更棒@@

From Noder

@zhangzeyang 初级 js基础 ,^o^

回到顶部