You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript实现数组元素展示及用户名(名+姓)显示问题

Complete Solution to Generate Full Usernames

Hey there! Let's finish up your getData function so it generates those full name-surname usernames you need. I'll also fix a couple of small issues in your existing code to make it cleaner and avoid unexpected behavior.

Here's the completed function:

function getData(count) { 
  // Fixed: Use const for non-changing arrays to avoid global scope leaks
  const names = ["Michał", "Jan", "Katarzyna", "Andrzej", "Józef", "Bartek", "Mikołaj", "Tomasz", "Julian", "Brajan", "Dżesika"];
  const surnames = ["Noga", "Kowalski", "Nowak", "Pazura", "Duda", "Komorowski", "Tomczyk", "Józefowicz", "Lechicki", "Goldberg"];
  const result = [];

  for (let i = 0; i < count; i++) { 
    // Get random indices for both name and surname
    const randomNameIndex = Math.floor(Math.random() * names.length);
    const randomSurnameIndex = Math.floor(Math.random() * surnames.length);
    
    // Combine name and surname into a full username
    const fullUsername = `${names[randomNameIndex]} ${surnames[randomSurnameIndex]}`;
    
    // Add to result array
    result.push(fullUsername);
  }

  // Return the array of usernames
  return result;
}

Key improvements and additions:

  • Fixed variable scope: Your original names array was declared without var, let, or const, which would make it a global variable (bad practice!). I changed all array declarations to const since they don't need to be modified.
  • Added random surname selection: We generate a random index for the surnames array just like we do for names.
  • Combined into full usernames: Using template literals (${}) makes it easy to concatenate the name and surname with a space.
  • Return the result: The function now returns the array of generated usernames so you can use them wherever you need to display them.

Example usage:

// Generate 5 random usernames
const usernames = getData(5);
console.log(usernames);
// Might output: ["Katarzyna Nowak", "Brajan Goldberg", "Jan Kowalski", "Michał Pazura", "Dżesika Tomczyk"]

Optional: Avoid duplicate username pairs

If you want to make sure you don't get the exact same name-surname combination multiple times, you can add a check to track used pairs:

function getData(count) { 
  const names = ["Michał", "Jan", "Katarzyna", "Andrzej", "Józef", "Bartek", "Mikołaj", "Tomasz", "Julian", "Brajan", "Dżesika"];
  const surnames = ["Noga", "Kowalski", "Nowak", "Pazura", "Duda", "Komorowski", "Tomczyk", "Józefowicz", "Lechicki", "Goldberg"];
  const result = [];
  const usedPairs = new Set();

  while (result.length < count) { 
    const randomNameIndex = Math.floor(Math.random() * names.length);
    const randomSurnameIndex = Math.floor(Math.random() * surnames.length);
    const fullUsername = `${names[randomNameIndex]} ${surnames[randomSurnameIndex]}`;
    
    if (!usedPairs.has(fullUsername)) {
      usedPairs.add(fullUsername);
      result.push(fullUsername);
    }
  }

  return result;
}

This uses a Set to keep track of pairs we've already added, so we only push unique combinations to the result.

内容的提问来源于stack exchange,提问作者mikitheboy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:32:12