javascript - Turn all function arguments values into one flatten array, to avoid array inside another array -


i'm working in function , need turn function's arguments in 1 array, even if 1 argument array (or list of values). i need array_merge() in php, keys not needed, values wanted.


edit 1:
i'm not looking how turn object arguments array. need values passed argument single array. if 1 or more arguments array or object, values need merged 1 array. example provided.


for example:
foo('bar', function, [1,2,3], [nodelist], window);
array => ['bar', function, 1, 2, 3, < htmlelement>, < htmlelement>, window]

pay attention on [nodelist], must turn htmlelements.

done far:
i've done function below, wanna know if better way. comments/hints appreciated.

function toarray(obj) {   var k = object.keys(obj);   var = 0, l = k.length;   if (isstring(obj) || l == 0 || obj === window) {     return obj;   } else {     var objs = [];     while (i < l) {       objs = objs.concat(toarray(obj[k[i]]));       i++;     }     return objs   } } function isstring(obj){   return (typeof obj === 'string' || obj instanceof string); } 

edit 1:
usage:

var arg1 = 'bar',     arg2 = [1,2,3],     arg3 = document.queryselectorall('body');  foo(arg1, arg2, arg3) {    var myarray = toarray(arguments);    //myarray: ['bar', 1, 2, 3, <body>] } 

use keyword arguments

http://www.w3schools.com/js/tryit.asp?filename=tryjs_function_arguments

function findmax() {    var i;    var max = -infinity;    (i = 0; < arguments.length; i++) {      if (arguments[i] > max) {        max = arguments[i];      }    }    return max;  }  document.getelementbyid("demo").innerhtml = findmax(4, 5, 6);
<p>finding largest number.</p>  <p id="demo"></p>

edit

this how flatten array.

var arg1 = document.queryselectorall('p');  var arg2 = 3;  var arg3 = [1, 2, [3, 2, 3, [45, arg1]]];  var arg4 = function() {};  var arg5 = {    a: {      b: 3,      c: 2    }  };  var arg6 = window;    function flatten() {      /*gets arguments past in*/      var arrays = arguments;      var last = 0;      /*flattens array*/      while (arrays.length != last) {        last = arrays.length;        arrays = [].concat.apply([], arrays);      }      /*maps nodelist array*/      arrays = arrays.map(function(obj) {        var arg1 = [];        if (isnodelist(obj)) {          [].foreach.call(obj, function(node) {            arg1.push(node);          });          return arg1;        } else {          return obj;        }      });      /*flattens node list arrays*/      last = 0;      while (arrays.length != last) {        last = arrays.length;        arrays = [].concat.apply([], arrays);      }      /*returns unique array*/      return unique(arrays);    }    /*makes array unique*/    function unique(a) {    return a.reduce(function(p, c) {      if (p.indexof(c) < 0) p.push(c);      return p;    }, []);  }    function isnodelist(obj) {    var objtype = {}.tostring.call(obj);    return (objtype === '[object nodelist]' ||      objtype === '[object htmlcollection]' ||      objtype === '[object object]' && /^\s?function/.test(obj.item)) && obj.length; // returns length of nodelist if true        };    document.getelementbyid('r').innerhtml = flatten(arg1, arg2, arg3, arg4, arg5, arg6);
<p id="s">    </p>  <p>    </p>  <span id='r'></span>


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -