Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 22, 2021 08:35 pm GMT

Flatten a nested array. Algorithms and Scripting

  • I've been saving these posts for a while now. I would always want to post something that I myself understand as well. Well Today we have a problem that needs us to flatten a nested array. We should also account for different levels of nesting.
  • Code:
function checkForArrayApproval(arr) {  return arr;}checkForArrayApproval([1, [2], [3, [[4]]]]);
  • Answer:
function steamrollArray(arr) {  let newArr = []  // Loop over array contents  for (let i = 0; i < arr.length; i++) {    if (Array.isArray(arr[i])) {      // Recursively flatten entries that are arrays      //  and push into the newArr      newArr.push(...steamrollArray(arr[i]));    } else {       // Copy contents that are not arrays      newArr.push(arr[i]);    }  }  return newArr;}// we could also do it this way://    while (arr.some(function(num) {//      return Array.isArray(num)//    })) {//      let i = 0//   arr = arr.flat()//   i += 1// }//   return arr;// }console.log(steamrollArray([1, [2], [3, [[4]]]])); // will display [1, 2, 3, 4]

Binary Code

  • In this following post we must return an English translated sentence of the passed binary string.
  • Code:
function binaryCode(str) {  return str;}binaryCode("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");
  • Answer:
function binaryCode(str) {  return str.split(" ").map(dataPoint => {    let characterPoint = parseInt(dataPoint, 2);    let decipheredLetter = String.fromCharCode(characterPoint);    return decipheredLetter;  }).join("")}console.log(binaryCode("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111")); will display Aren't bonfires fun!?// 2    Binary numeral system   Used internally by nearly all computers, is base 2. The two digits are "0" and "1", expressed from switches displaying OFF and ON, respectively.// The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.// Ex: // const array1 = [1, 4, 9, 16];// // pass a function to map// const map1 = array1.map(x => x * 2);// console.log(map1);// // expected output: Array [2, 8, 18, 32];

It is a sunday, currently 4:15 in the afternoon, today we're going to flatten a nested array.

  • Also don't forget that you must consider for different levels of nesting.

  • Code:

function nestedArr(arr) {  return arr;}nestedArr([1, [2], [3, [[4]]]]);
  • Answer:
function nestedArr(arr) {  let newArr = []  for (let i = 0; i < arr.length; i++) {    if (Array.isArray(arr[i])) { // wants to check each element if it is an Array or not.      newArr = newArr.concat(nestedArr(arr[i])) // we're using recursion because we don't know how many levels it will be so we want it to call itself whenever it is needed. call for each array, if it is we go deeper in the nested levels    } else {    newArr.push(arr[i]) // if it is not an array (base case)    }  }  return newArr;}console.log(nestedArr([1, [2], [3, [[4]]]])); will display [1, 2, 3, 4] 

Original Link: https://dev.to/rthefounding/flatten-a-nested-array-algorithms-and-scripting-528j

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To