Home > Web Front-end > JS Tutorial > js uses concat and sort to splice N arrays_javascript skills

js uses concat and sort to splice N arrays_javascript skills

WBOY
Release: 2016-05-16 15:19:00
Original
1485 people have browsed it

This article analyzes the method of splicing N arrays using concat and sort in js. Share it with everyone for your reference, the details are as follows:

This is an expansion of an interview question from a large company. It is just a solution, but it is not sure whether it is efficient or not, but the effect is indeed achieved!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title></title>
  <script type="text/javascript" >
    function concatAndSortArray(array1, array2) {
      if (arguments.length < 2) {
        return;
      }
      var arg = null;
      var array = [];
      for (var i = 0; i < arguments.length; i++) {
        arg = arguments[i];
        if (typeof arg === "object" && arg.length > 0) {
          array = array.concat(arg);
        }
      }
      array.sort(function (arg1, arg2) {
        if (arg1 > arg2) {
          return 1;
        } else {
          return -1;
        }
      });
      return array;
    }
    var array1 = [1, 2, 3, 4, 5, 15];
    var array2 = [10, 12, 13, 25, 35];
    var array = concatAndSortArray(array1, array2);
    console.log(array);
  </script>
  <style type="text/css" >
  #tx { line-height:30px; font-weight:bolder; text-align:center; background:#DDDDDD; margin-top:10px;}
  </style>
</head>
<body>
<div id="tx">haha</div>
<hr />
<div id="container">
</div>
</body>
</html>

Copy after login

Readers who are interested in more content related to JavaScript arrays and strings can check out the special topics on this site: "Summary of JavaScript array operation skills" and "Summary of JavaScript string related operations

I hope this article will be helpful to everyone in JavaScript programming.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template