批量制造数据
// 方法一、
function createData() {
const data = [];
for (let i = 0; i < 1000; i++) {
data.push({
name: `name${i + 1}`,
});
}
return data;
}
const data = createData();
console.log(data);
// 方法二、创建空数组,填充full,然后map
function createData() {
// 如果不 fill 循环默认会跳过空值
return new Array(1000).fill(null).map((v, i) => ({ name: `name${i + 1}` }));
}
const data = createData();
console.log(data);
// 方法三、Array.from 第二个初始化函数返回数据
function createData() {
return Array.from({ length: 1000 }, (v, i) => ({ name: `name${i + 1}` }));
}
const data = createData();
console.log(data);
二维数据去重(如果key值一样,则认为对象一样)
const uniqueArray = (array: any[], key: any) => {
const keyValues = new Set();
return array.filter((item) => {
if (!keyValues.has(item[key])) {
keyValues.add(item[key]);
return true;
}
return false;
})
}
const arr = [{
id: "1",
name: "jack"
}, {
id: "1",
name: "tony"
}, {
id: "2",
name: "jacky"
}]
console.log(uniqueArray(arr, 'id'));