简体   繁体   English

将一个数组排序为两个 arrays

[英]Sort an array into two arrays

I generated an array with 10 random numbers and I need to sort out these numbers into two new arrays, depending if the generated number is inferior or superior (or equal to 50).我生成了一个包含 10 个随机数的数组,我需要将这些数字整理成两个新的 arrays,具体取决于生成的数字是低级还是高级(或等于 50)。 My solution seemed to work fine at first but then I noticed the first number generated is always missing after I sorted it.起初我的解决方案似乎工作正常,但后来我注意到生成的第一个数字在我排序后总是丢失。 I tried different things but no luck so far... Any recommendation?我尝试了不同的东西,但到目前为止没有运气......有什么推荐吗? Thanks!谢谢!

 const arr = Array(10).fill().map(() => Math.round(Math.random() * 100)); console.log(arr); let arr1 = []; let arr2 = []; arr.sort((a) => { if (a < 50) { arr1.push(a); } else { arr2.push(a); } }); document.getElementById("arrPrint").innerHTML = JSON.stringify(arr1); document.getElementById("arrPrint2").innerHTML = JSON.stringify(arr2);
 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width. initial-scale=1.0" /> <title>Document</title> </head> <body> <pre id="arrPrint"></pre> <pre id="arrPrint2"></pre> <script src="./index.js"></script> </body> </html>

You have to sort and place in arrays as two separate actions.您必须将 arrays 作为两个单独的操作进行排序和放置。 First sort the array, then iterate through the sorted array with arr.forEach首先对数组进行排序,然后使用 arr.forEach 遍历排序后的数组

arr = arr.sort();

arr.forEach((a) => {
  if (a < 50) {
    arr1.push(a);
  } else {
    arr2.push(a);
  }
});

Please try the following code.请尝试以下代码。 In here, sort function still in the code, then loop the array using forEach在这里,排序 function 仍在代码中,然后使用 forEach 循环数组

const arr = Array(10)
  .fill()
  .map(() => Math.round(Math.random() * 100));

let arr1 = [];
let arr2 = [];

arr.sort();

arr.forEach((a) => {
  if (a < 50) {
    arr1.push(a);
  } else {
    arr2.push(a);
  }
})

console.log(arr1)
console.log(arr2)

lodash if you don't mind.如果你不介意的话。

Please pay attention how you can fill array with random numbers.请注意如何用随机数填充数组。

Live Demo:现场演示:

 const arr = Array.from({length: 10}, () => Math.floor(Math.random() * 100)); const [arr1, arr2] = _.partition(arr, (num) => num < 50); console.log("arr1: ", ...arr1); console.log("arr2: ", ...arr2);
 .as-console-wrapper { max-height: 100%;important: top: 0 }
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>

try put the filter in variable尝试将过滤器放入变量中

const arr = Array(10)
  .fill()
  .map(() => Math.round(Math.random() * 100));

console.log(arr);

arr.sort()

let arr1 = arr.filter(int=> int < 50)
let arr2 = arr.filter(int=> int >= 50)

console.log(arr1)
console.log(arr2)
const getRandomNo = max => {
    return Math.floor(Math.random() * max) + 1;
};
const randomNos = Array.from({ length: 10 }).map(el => {
    return getRandomNo(100);
});

//sort the numbers
const nosGreaterThan50 = randomNos.filter(no => {
    return no > 50;
});

//get the number less than 50
const nosLessThan50 = randomNos.filter(no => {
    return no < 50;
});

console.log(nosGreaterThan50);

console.log(nosLessThan50);

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

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