Quick actions

cmd+k|ctrl+k

Navigation

Languages

array reduce

Snippet info

Language

JavaScript

Visibility

public

Author

thoriqnugroho3

Created

2020-06-29T03:03:18Z

Updated

2020-06-29T03:06:05Z

const numArray = [1, 2, [3, 10, [11, 12]], [1, 2, [3, 4]], 5, 6];
const exArr = [{0: "new"}, {2: "modified"}, {1: "unmodified"}];

const getMapping = exArr.reduce((acc, item) => ({ ...acc, ...item }));
const arrMap = numArray.reduce((acc, item) => ({ ...acc, ...item }));

console.log(getMapping)
console.log(arrMap)

function flattenArray(data) {
  // our initial value this time is a blank array
  const initialValue = [];

  // call reduce on our data
  return data.reduce((total, value) => {
    // if the value is an array then recursively call reduce
    // if the value is not an array then just concat our value
    return total.concat(Array.isArray(value) ? flattenArray(value) : value);
  }, initialValue);
}

console.log(flattenArray(numArray))
INFO