Home > Mobile >  Sort an array into two arrays
Sort an array into two arrays

Time:09-21

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). 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>

CodePudding user response:

Please try the following code. In here, sort function still in the code, then loop the array using 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)

CodePudding user response:

You have to sort and place in arrays as two separate actions. First sort the array, then iterate through the sorted array with arr.forEach

arr = arr.sort();

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

CodePudding user response:

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/[email protected]/lodash.min.js"></script>

CodePudding user response:

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)

CodePudding user response:

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);
  • Related