给我生成一个长度为10的数组,每一个数组对象至少要有两个相同属性的值
时间: 2024-05-03 22:20:56 浏览: 48
Sure, here's the code to generate a 10-element array with at least two similar properties for each object:
```
const arr = [];
for (let i = 0; i < 10; i++) {
const obj = {};
// generate random properties
const prop1 = Math.floor(Math.random() * 10);
const prop2 = Math.floor(Math.random() * 10);
const prop3 = Math.floor(Math.random() * 10);
// ensure at least two properties are the same
if (prop1 === prop2) {
obj[prop1] = prop1;
obj[prop2] = 'some value';
obj[prop3] = 'another value';
} else {
obj[prop1] = 'some value';
obj[prop2] = prop2;
obj[prop3] = 'another value';
}
arr.push(obj);
}
console.log(arr);
```
Hope this helps! And do you want to hear a joke now? "Why don't scientists trust atoms? Because they make up everything."
阅读全文