繁体   English   中英

如何在数组上添加项目并替换数组上的项目?

[英]How do I add item on array and replace item on array?

编写一个程序来填充客人名单。 如果客人被邀请,请让客人入场; 如果没有,把它放在垃圾上。

我的代码:

const admit = document.getElementById("admit");
const refuse = document.getElementById("refuse");
admit.textContent = "Admit: ";
refuse.textContent = "Refuse: ";
const people = [
  "Chris",
  "Anne",
  "Colin",
  "Terri",
  "Phil",
  "Lola",
  "Sam",
  "Kay",
  "Bruce",
];

for (let i = 0; i < people.length; i++) {
  if (people[i] === "Phil" || people[i] === "Lola") {
    refuse.textContent += people[i] + ",";
  } else {
    admit.textContent += people[i] + ", ";
  }
}

我得到了程序的工作。

Admit:  Chris, Anne, Collin,Terri,Sam,Kay,
Refuse:  Phil,Lola,

但我正在尝试解决如何添加and替换,. .

这是我想要的 output:

Admit:  Chris, Anne, Collin,Terri, Sam, Kay, and Bruce.
Refuse:  Phil and Lola.

 const people = ['Chris', 'Anne', 'Colin', 'Terri', 'Phil', 'Lola', 'Sam', 'Kay', 'Bruce']; const admit = []; const refuse = []; for (let person of people) { (person === 'Phil' || person === 'Lola'? refuse: admit).push(person); } const toText = (array) => { let multipleNames = array.length > 1; return `${array.slice(0, -1).join(", ")}${multipleNames? " and ": ""}${array.slice(-1)[0]}.`; }; admit.textContent = `Admit: ${toText(admit)}`; refuse.textContent = `Refuse: ${toText(refuse)}`; console.log(admit.textContent); console.log(refuse.textContent);

你可以试试下面的代码。 有条件地添加 and 和逗号。

const admit = document.getElementById('admit');
const refuse = document.getElementById('refuse');
admit.textContent = 'Admit: ';
refuse.textContent = 'Refuse: ';
const people = ['Chris', 'Anne', 'Colin', 'Terri', 'Phil', 'Lola', 'Sam', 'Kay', 'Bruce'];

for (let i = 0; i <= people.length; i++) {
if (people[i] === 'Phil' || people[i] === 'Lola') {
      refuse.textContent += (refuse.length == 1) ' and ' + people[i] : people[i];
  }else{
      admit.textContent += (people.length == i) ' and ' + people[i] : people[i] + ',';
  }
}

试试这个:

const admit = document.getElementById("admit");
const refuse = document.getElementById("refuse");
admit.textContent = "Admit: ";
refuse.textContent = "Refuse: ";
const people = ["Chris", "Anne", "Colin", "Terri", "Phil", "Lola", "Sam", "Kay", "Bruce"];
let refusedPeople = [];
let admitedPeople = [];
people.forEach((person) => {
  person === "Phil" || person === "Lola" ? refusedPeople.push(person) : admitedPeople.push(person);
});

const genrateConcatString = (arr, person, index) => {
  let text = "";
  if (index === arr.length - 1) {
    arr.length === 1 ? (text += person + ".") : (text += " and " + person + ".");
  } else {
    index === arr.length - 2 ? (text += person) : (text += person + ", ");
  }
  return text;
};
refusedPeople.forEach((person, index) => {
  refuse.textContent += genrateConcatString(refusedPeople, person, index);
});

admitedPeople.forEach((person, index) => {
  admit.textContent += genrateConcatString(admitedPeople, person, index);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM