简体   繁体   English

如何从另一个数组中的 pop 推送到一个数组?

[英]How can I push to one array from a pop in another?

I am writing a program to emulate a kid's football, baseball, stickball;我正在编写一个程序来模拟孩子的足球、棒球、棒球; any team game.任何团队游戏。 10 kids in an array. 10 个孩子在一个数组中。 Each kid has a random power number.每个孩子都有一个随机的幂数。 You know you always picked the best kids first, then so on and so on.你知道你总是先挑选最好的孩子,然后依此类推。 So my script generates a random number for the kid's power.所以我的脚本为孩子的力量生成了一个随机数。 Then sorts through the array to determine the two highest kid's powers and places them at the end of the array and pops them out into two new arrays called teamOne and teamTwo.然后对数组进行排序以确定两个最高孩子的力量并将它们放在数组的末尾并将它们弹出到两个名为 teamOne 和 teamTwo 的新数组中。 Then they toss a coin to see who picks first, pretty much by an if-else loop with a while nested inside the if.然后他们抛硬币看谁先选,这几乎是通过 if-else 循环和嵌套在 if 中的 while 循环。

My sort works, as does the pop of the two team captains into the two new team arrays.我的排序有效,两名队长加入两个新队阵也是如此。 My coin toss works, but then I am at loss.我的抛硬币成功了,但后来我不知所措。 So I am asking for some direction or instructional material suggestions for this problem?所以我想为这个问题寻求一些指导或教学材料建议?

Here is my code, and thank you.这是我的代码,谢谢。


// generate a random number
let getRandNum = function (start, range) {
  let getRand = (Math.random() * range) + start;
  while (getRand > range) {
    getRand = (Math.random() * range) + start;
  };
  return getRand;
};

// array of 10 kids
var Kids = [{
    name: "Bobby",
    random: getRandNum(1, 10)
  },
  {
    name: "Frankie",
    random: getRandNum(1, 10)
  },
  {
    name: "Juan",
    random: getRandNum(1, 10)
  },
  {
    name: "Sid",
    random: getRandNum(1, 10)
  },
  {
    name: "Ellie",
    random: getRandNum(1, 10)
  },
  {
    name: "Harry",
    random: getRandNum(1, 10)
  },
  {
    name: "Chester",
    random: getRandNum(1, 10)
  },
  {
    name: "Lucio",
    random: getRandNum(1, 10)
  },
  {
    name: "Nancy",
    random: getRandNum(1, 10)
  },
  {
    name: "Kim",
    random: getRandNum(1, 10)
  }
];

// sort based on random number with thanks to:
//  https://www.sitepoint.com/sort-an-array-of-objects-in-javascript

function compareValues(key, order = 'asc') {
  return function innerSort(a, b) {
    if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
      // property doesn't exist on either object
      return 0;
    }

    const varA = (typeof a[key] === 'string') ?
      a[key].toUpperCase() : a[key];
    const varB = (typeof b[key] === 'string') ?
      b[key].toUpperCase() : b[key];

    let comparison = 0;
    if (varA > varB) {
      comparison = 1;
    } else if (varA < varB) {
      comparison = -1;
    }
    return (
      (order === 'desc') ? (comparison * -1) : comparison
    );
  };
}

console.log(Kids.sort(compareValues('random', )));

const teamOne = Kids.pop();
const teamTwo = Kids.pop();

console.log(teamOne);
console.log(teamTwo);
console.log(Kids);

let coinToss = "Heads"

if (Math.floor((Math.random() * 2) + 1) == 2) {
  coinToss = "Tails";
};

console.log(coinToss);

if (coinToss = "Heads") {
  let x = 4;
  while (x > 0) {
    teamOne.push([Kids.pop()]);
    teamTwo.push([Kids.pop()]);
    x -= x;
  };
} else {
  let x = 4;
  while (x > 0) {
    teamTwo.push([Kids.pop()]);
    teamOne.push([Kids.pop()]);
    x -= x;
  };
  console.log("_____________");
  console.log(coinSide);
  console.log("_____________");
  console.log(teamOne.name);
  console.log("_____________");
  console.log(teamTwo.name);
};

The problem is that you are not treating teamOne and teamTwo as arrays, so you can not use "push".问题是您没有将 teamOne 和 teamTwo 视为数组,因此您不能使用“push”。 On top of that, the Kids array objects have two values - "name" and "random" - so, using "push" would add a new kid as an object.最重要的是,Kids 数组对象有两个值——“name”和“random”——因此,使用“push”会添加一个新的孩子作为对象。

So, firstly, you need to define teamOne and teamTwo as arrays and not strings.因此,首先,您需要将 teamOne 和 teamTwo 定义为数组而不是字符串。 And, secondly, you need to extract the "name" property from the Kids objects (assuming that you were only using "random" for sorting purposes and no longer require it's value).其次,您需要从 Kids 对象中提取“名称”属性(假设您仅使用“随机”进行排序并且不再需要它的值)。 And, finally, you have not assigned a name to each team which, if you used an array for them, you could now do.而且,最后,您还没有为每个团队分配一个名称,如果您为他们使用数组,您现在可以这样做。

Try something like:尝试类似:

// generate a random number
let getRandNum = function (start, range) {
  let getRand = (Math.random() * range) + start;
  while (getRand > range) {
    getRand = (Math.random() * range) + start;
  };
  return getRand;
};

// array of 10 kids
var Kids = [{
    name: "Bobby",
    random: getRandNum(1, 10)
  },
  {
    name: "Frankie",
    random: getRandNum(1, 10)
  },
  {
    name: "Juan",
    random: getRandNum(1, 10)
  },
  {
    name: "Sid",
    random: getRandNum(1, 10)
  },
  {
    name: "Ellie",
    random: getRandNum(1, 10)
  },
  {
    name: "Harry",
    random: getRandNum(1, 10)
  },
  {
    name: "Chester",
    random: getRandNum(1, 10)
  },
  {
    name: "Lucio",
    random: getRandNum(1, 10)
  },
  {
    name: "Nancy",
    random: getRandNum(1, 10)
  },
  {
    name: "Kim",
    random: getRandNum(1, 10)
  }
];

// sort based on random number with thanks to:
//  https://www.sitepoint.com/sort-an-array-of-objects-in-javascript

function compareValues(key, order = 'asc') {
  return function innerSort(a, b) {
    if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
      // property doesn't exist on either object
      return 0;
    }

    const varA = (typeof a[key] === 'string') ?
      a[key].toUpperCase() : a[key];
    const varB = (typeof b[key] === 'string') ?
      b[key].toUpperCase() : b[key];

    let comparison = 0;
    if (varA > varB) {
      comparison = 1;
    } else if (varA < varB) {
      comparison = -1;
    }
    return (
      (order === 'desc') ? (comparison * -1) : comparison
    );
  };
}

console.log(Kids.sort(compareValues('random', )));

let teamOne = [];
let teamTwo = [];

let kidName1 = {};
let kidName2 = {};

kidName1 = Kids.pop();
teamOne[0] = kidName1.name;
kidName2 = Kids.pop();
teamTwo[0] = kidName2.name;
teamOne.name = "Team 1";
teamTwo.name = "Team 2";

console.log(teamOne);
console.log(teamTwo);
console.log(Kids);

let coinToss = "Heads"

if (Math.floor((Math.random() * 2) + 1) == 2) {
  coinToss = "Tails";
};

console.log(coinToss);

for (let i = 0; i < 4; i++) {
    kidName1 = Kids.pop();
    kidName2 = Kids.pop();
    if (coinToss == "Heads") {
        teamOne.push(kidName1.name);
        teamTwo.push(kidName2.name);
    } else {
        teamOne.push(kidName2.name);
        teamTwo.push(kidName1.name);
    }
};
console.log("_____________");
console.log(coinToss);
console.log("_____________");
console.log(teamOne.name);
console.log("_____________");
console.log(teamTwo.name);
console.log("_____________");
console.log(teamOne.toString());
console.log("_____________");
console.log(teamTwo.toString());

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

相关问题 如何将一个阵列推入另一个阵列? - How can i push a array in another array? 如何将一个数组推送到另一个数组 - How can I push an array to another array 当我在 Javascript 中单击提交按钮时,我们如何从一个表中弹出元素并将相同的元素推送到另一个表中? - How we can pop element from one table and push the same element in other table when i click the submit button in Javascript? 我怎样才能将一个数组推到另一个数组来制作2级数组? - How can i push an array to another array to make a 2 level array? 如何从一个类的数组推送到javascirpt中的另一个数组? - how to push from one array of class to another array in javascirpt? 如何将由 for 循环制作的复选框中选中的数组值推送到另一个数组? - How can I push array values checked in checkboxes made from a for loop to another array? 如何将数据从数组/对象推送到另一个数组/对象? - How can I push data from an array/object into another array/object? 如何将各个类依次推入数组? - How do I .push separate classes into an array one after another? 如何将数组从一个 function 传递到 class 中的另一个? - How can I pass an array from one function to another in a class? 如何将数组作为一个元素推入另一个数组 - How to push array into another array as one element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM