使用原生JS造一些简单的数据,详细代码如下:
let arr = ['Attention', 'Ace', 'Aorger'];
arr = arr.map(item => {
return {
name: item,
age: Math.floor(Math.random() * 50),
gender: Math.round(Math.random()) === 1 ? "男" : "女"
}
});
console.log(arr);
上面的代码可以简化为:
let arr = ['Attention', 'Ace', 'Aorger'];
arr = arr.map(item => ({
name: item,
age: Math.floor(Math.random() * 50),
gender: Math.round(Math.random()) ? "男" : "女"
}));
console.log(arr);
省略return后,在对象外加一对小括号,将函数写法转换成对象写法。 在本例中,三元表达式中的 === 1 也可以省略。
输出结果如下图所示:
