对象和属性的随机数组

Random Array of Objects & Properties

所以我在下面的代码中有一个数组,但我对分配的要求之一是有一个非常大的对象数组,大约 10,000 个对象。我的老师说计算机可以根据你想要的数量为你不断生成随机对象,在我的例子中是 10,000。我只是对如何做到这一点感到困惑,非常感谢任何帮助或建议,谢谢!

  class Shoe {
      constructor(name, price, type) {
        this.name = name;
        this.price = price;
        this.type = type;
      }
  }

  var shoes = [
     new Shoe('Nike AirMax 90', '120', 'Casual'),
     new Shoe('Jordan Retro 1', '110', 'Casual'),
     new Shoe('Jadon Doc Martens', '250', 'Seasonal boots'),
     new Shoe('Adidas X Ghosted', '110', 'Athletic'),
     new Shoe('Nike Vapourmax Flyknit', '250', 'Casual'),
     new Shoe('Aldo Loafers', '130', 'Formal'),
     new Shoe('Timberlands', '199', 'Seasonal boots'),
     new Shoe('Converse High Tops', '70', 'Casual'),
     new Shoe('Converse Low Tops', '80', 'Casual'),
     new Shoe('Adidas NMDs', '110', 'Athletic'),
     new Shoe('Heels', '130', 'Formal'),
     new Shoe('Nike AirForce', '150', 'Casual')
  ];

  function bubbleSort(shoes) {
    var swapped;
    do {
      swapped = false;
      for (var i = 0; i < shoes.length - 1; i++) {
        if (+shoes[i].price > +shoes[i + 1].price) {
           var temp = shoes[i];
           shoes[i] = shoes[i + 1];
           shoes[i + 1] = temp;
           swapped = true;
        }
      }
    } while (swapped);
    return shoes;
   }

   bubbleSort(shoes);
   console.log('Bubble Sort:\n', shoes);

这是循环的完美工作。您必须创建生成随机名称、价格和类型的辅助函数。

对于 name,我们将使用:

function randomName(n) {
  let letters = "abcdefghijklmnopqestuvwxyz";
  let name = "";
  
  for(let i = 0; i < n; i++) {
    name += letters[Math.floor(Math.random() * letters.length)];
  }

  return name;
}

此函数生成特定长度的字符串,但这些字符串只是乱码,我想这不是很重要。

对于 price,我们将使用:

function randomNumber(min, max) {
  return Math.floor(Math.random() * (max - min) + min);
}

对于 type,我们将只使用 randomName

然后我们使用 for 循环来填充 shoes 数组,如下所示:

var shoes = [];

for(let i = 0; i < 10000; i++) {
  shoes.push(new Shoe(randomName(20), randomNumber(50, 5000), randomName(7)));
}

演示:

class Shoe {
  constructor(name, price, type) {
    this.name = name;
    this.price = price;
    this.type = type;
  }
}

function randomName(n) {
  let letters = "abcdefghijklmnopqestuvwxyz";
  let name = "";

  for (let i = 0; i < n; i++) {
    name += letters[Math.floor(Math.random() * letters.length)];
  }

  return name;
}

function randomNumber(min, max) {
  return Math.floor(Math.random() * (max - min) + min);
}


var shoes = [];

for (let i = 0; i < 100; i++) {
  shoes.push(new Shoe(randomName(20), randomNumber(50, 5000), randomName(7)));
}

console.log(shoes);

编写一个随机创建鞋子的函数,并在循环中调用它。

class Shoe {
  constructor(name, price, type) {
    this.name = name;
    this.price = price;
    this.type = type;
  }
}

function random_words(length) {
  let word = "";
  const chars = " abcdefghijklmnopqrstuvwxyz";
  let last_space = true;
  for (let i = 0; i < length; i++) {
    let char = chars[Math.floor(Math.random() * chars.length)];
    word += last_space ? char.toUpperCase() : char;
    last_space = char == " ";
  }
  return word;
}

function random_shoe() {
  let name = random_words(30);
  let price = Math.floor(Math.random() * 200);
  let style = random_words(10);
  return new Shoe(name, price, style);
}

let shoes = [];
for (let i = 0; i < 10; i++) {
  shoes.push(random_shoe());
}

console.log(shoes);

class Shoe {
  constructor(name, price, type) {
    this.name = name;
    this.price = price;
    this.type = type;
  }
}

names = ["Nike", "Adidas", "Puma"];
types = ["Running", "Casual", "Walking", "Training"];
pricesRange = [1, 1000];

const randomShoes = [];
const numberOfShoes = 10;

const randomInteger = () => parseInt(Math.random() * (pricesRange[1] - pricesRange[0]) + pricesRange[0])

const randomArrayItem = arr => {
  const randomIndex = parseInt(Math.random() * (arr.length - 1));
  return arr[randomIndex];
}

for (let i = 0; i < numberOfShoes; i++) {
  const name = randomArrayItem(names);
  const price = randomInteger();
  const type = randomArrayItem(types);
  const shoe = new Shoe(name, price, type)
  randomShoes.push(shoe);
}

randomShoes.forEach(shoe => console.log(shoe));

您可以像这样创建一个包含 10,000 只鞋的数组,每只鞋的价格都是随机的:

class Shoe {
  constructor(name, price, type) {
    this.name = name;
    this.price = price;
    this.type = type;
  }
}
var names = ["Nike AirMax 90", "Jordan Retro 1", "Jadon Doc Martens"]; // can be more
var types = ["Casual", "Athletic", "Formal"]; // can be more
   
var shoes = [];
for (var i = 0; i < 10000; i++) {
  shoes.push(new Shoe(
    names[Math.floor(Math.random() * names.length) -1], 
    Math.floor(Math.random() * 10000), 
    types[Math.floor(Math.random() * names.length) -1])
  );
}

console.log(shoes)

这会让你做冒泡排序练习。数据可能不是很真实,但我认为这可能并不重要。