JavaScript Array Properties, Methods (1)

JavaScript Array properties, Methods (2)

JavaScript Array properties, Methods (3)

usereduceconcat

  1. There’s only flattening of two series
let arr = [1.2[3.4]].let result1 = arr.flat();
console.log(result1);
// output[Array]: [1, 2, 3, 4]

let result2 = arr.reduce((acc, val) = > acc.concat(val), []);
console.log(result2);
// output[Array]: [1, 2, 3, 4]

const flattened = arr= >[].concat(... arr);let result3 = flattened(arr);
console.log(result3)
// output[Array]: [1, 2, 3, 4]
Copy the code
  1. usereduce,concatAnd recursive infinite unnesting of multiple nested arrays
let arr1 = [1.2.3[1.2.3.4[2.3.4]]];
const flattenDeep = arr1= > {
  return arr1.reduce(
    (acc, val) = >
    Array.isArray(val) ? acc.concat(flattenDeep(val)) : acc.concat(val),
    []
  );
};
let result = flattenDeep(arr1);
console.log(result)
// output[Array]: [1, 2, 3, 1, 2, 3, 4, 2, 3, 4]
Copy the code
  1. Instead of using recursion, usestackInfinite unnesting of multiple nested arrays
let arr1 = [1.2.3[1.2.3.4[2.3.4]]];
const flatten = input= > {
  const stack = [...input];
  const res = [];
  while (stack.length) {
    // Use pop to fetch and remove values from the stack
    const next = stack.pop();
    if (Array.isArray(next)) {
      // Use push to send back elements in the inner array without changing the original inputstack.push(... next); }else{ res.push(next); }}// Use reverse to restore the order of the original array
  return res.reverse();
};
let result = flatten(arr1);
console.log(result);
// output[Array]: [1, 2, 3, 1, 2, 3, 4, 2, 3, 4]
Copy the code
  1. Recursive version of anti-nesting
let arr1 = [1.2.3[1.2.3.4[2.3.4]]];
const flatten = array= > {
  let flattend = [];
  (function flat(array) {
    array.forEach(el= > {
      if (Array.isArray(el)) flat(el);
      else flattend.push(el);
    });
  })(array);
  return flattend;
};
let result = flatten(arr1);
console.log(result)
// output[Array]: [1, 2, 3, 1, 2, 3, 4, 2, 3, 4]
Copy the code

reduce + concat + isArray + recursivity

let arr1 = [1.2.3[1.2.3.4[2.3.4]]];
let arr = [1.2[3.4[5.6]]];

const flatDeep = arr= > {
   return arr.reduce((acc, val) = > acc.concat(Array.isArray(val) ? flatDeep(val) : val), []);
};

let result = flatDeep(arr1);
console.log(result)
// output[Array]: [1, 2, 3, 1, 2, 3, 4, 2, 3, 4]
Copy the code

Use a stack

let arr1 = [1.2.3[1.2.3.4[2.3.4]]];
// Use stack for non-recursive depth flattening
const flatten = input= > {
  const stack = [...input];
  const res = [];
  while (stack.length) {
    const next = stack.pop();
    if (Array.isArray(next)) { stack.push(... next); }else{ res.push(next); }}// Use reverse to restore the order of the original array
  return res.reverse();
};

let result = flatten(arr1);
console.log(result);
// output[Array]: [1, 2, 3, 1, 2, 3, 4, 2, 3, 4]
Copy the code

Use Generator Functions

let arr1 = [1.2.3[1.2.3.4[2.3.4]]];
function* flatten(array) {
  for (const item of array) {
    if (Array.isArray(item)) {
      yield* flatten(item);
    } else {
      yielditem; }}}const flattened = [...flatten(arr1)];

console.log(flattened);
// output[Array]: [1, 2, 3, 1, 2, 3, 4, 2, 3, 4]
Copy the code