preface

Sometimes the nesting layers of the data structure back to the front end are too deep and the access to the properties inside is too complex, resulting in the flat way of deep arrays and objects.

Array flattening

Simple version implementation:

 function flatten(array, result) {
            result = result || [];
            array.forEach(element= > {
                if (Array.isArray(element)) {
                    flatten(element, result);
                } else{ result.push(element); }});return result;
        }
        var arr = [2[3.4], [[5],
                [6]]];var result = flatten(arr);
        console.log(result);
Copy the code

The output is:

Underscore ()

 // Internal implementation of a recursive `flatten` function.
  var flatten = function(input, shallow, strict, output) {
    output = output || [];
    var idx = output.length;
    for (var i = 0, length = getLength(input); i < length; i++) {
      var value = input[i];
      if (isArrayLike(value) && (_.isArray(value) || _.isArguments(value))) {
        // Flatten current level of array or arguments object.
        if (shallow) {
          var j = 0, len = value.length;
          while (j < len) output[idx++] = value[j++];
        } else{ flatten(value, shallow, strict, output); idx = output.length; }}else if (!strict) {
        output[idx++] = value;
      }
    }
    return output;
  };
Copy the code

The object is flat

/* 题目*/
var entryObj = {
	a: {
		b: {
			c: {
				dd: 'abcdd'
			}
		},
		d: {
			xx: 'adxx'
		},
		e: 'ae'Var outputObj = {'a.b.c.dd': 'abcdd'.'a.d.xx': 'adxx'.'a.e': 'ae'
}
Copy the code

Simply flatten a deep object

function flattenObj(obj, tempKey, resultObj) {
            tempKey = tempKey || ' ';
            resultObj = resultObj || {};
            for (let key in obj) {
                var value = obj[key];
                if (typeof value == 'object') {
                    tempKey = tempKey + key + '. ';
                    flattenObj(value, tempKey, resultObj);
                    tempKey = ' ';
                } else{ tempKey = tempKey + key; resultObj[tempKey] = value; }};return resultObj;
        }
Copy the code

The following is the comment section. Paste it here:

function flat(obj, key = "", res = {}, isArray = false) { 
  for (let [k, v] of Object.entries(obj)) { 
    if (Array.isArray(v)) { 
      let tmp = isArray ? key + "[" + k + "]" : key + k 
      flat(v, tmp, res, true)}else if (typeof v === "object") { 
      let tmp = isArray ? key + "[" + k + "]." : key + k + "." 
      flat(v, tmp, res) 
    } else { 
      let tmp = isArray ? key + "[" + k + "]" : key + k 
      res[tmp] = v 
    } 
  } 
  return res 
}
Copy the code

The test code

 var entryObj = {
            a: {
                b: {
                    c: {
                        dd: 'abcdd'
                    }
                },
                d: {
                    xx: 'adxx'
                },
                e: 'ae'}}; var result = flattenObj(entryObj); console.log(result);Copy the code

The output