Lodash.js – flatten() function

A simple Lodash.js example of the flatten() function, which flattens one level down the nested arrays.
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <meta name="description" content="Tizen Angular.js example"/>
    <title>Tizen lodash.js example</title>
    <script src="js/lodash.min.js"></script>
</head>
<body>
<div id="first">
</div>
<br>
<div id="second">
</div>

<script>

    var firstDiv = document.getElementById('first');
    var secondDiv = document.getElementById('second');

    var firstValue = [1, 0, 2, [[3, 5], [5, 7, 8]], 12];

  firstDiv.innerHTML = "Having the nested arrays:\n\n" + firstValue + " and " + firstValue[3] + " and " + firstValue[3][0] + " and " + firstValue[3][1] + " we fill flatten the arrays one level below...";

    var result = _.flatten(firstValue);

  secondDiv.innerHTML = "...and we get: ";

  for (var i=0; i < result.length; i++) {
    document.write("<br>result element at index: " + i + " is " + result[i]);
  }

</script>
</body>

</html>

Responses

0 Replies