Problems with arguments

0

I am asked to process two or more arrays sent from a function. But the function only sends one argument. I do not know if the problem is wrongly formulated or if it is really possible to do so. This is what I have done so far:

function uniteUnique(arr) {
  var newArr=[];
  var args = Array.from(arr);

  for(i=0;i<arr.length;i++){
    if(Array.isArray(arr[i])){
      for(j=0;j<arr[i].length;j++){
        if(!newArr.includes(arr[i][j])){
          newArr.push(arr[i][j]);
        }
      }
    }else{
      newArr.push(arr[i]);
    }
  }
  return newArr;
}

uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
    
asked by AlienMind 31.01.2018 в 00:58
source

2 answers

0

The only solution would be to send an array (array) of fixes, so you only receive one parameter but you can manipulate the ones you want, as long as they are inside the argument

    
answered by 31.01.2018 в 01:04
0

Let's see if this helps you, all I did was add [before and] after your declaration of arrangement ... So I group all your arrangements into one ...

function uniteUnique(arr) {
  var newArr=[];
  var args = Array.from(arr);

  alert(args);

  for(i=0;i<arr.length;i++){
    if(Array.isArray(arr[i])){
      for(j=0;j<arr[i].length;j++){
        if(!newArr.includes(arr[i][j])){
          newArr.push(arr[i][j]);
        }
      }
    }else{
      newArr.push(arr[i]);
    }
  }
  return newArr;
}

$('#procesar').click(function(){
  var arr = uniteUnique([[1, 3, 2], [5, 2, 1, 4], [2, 1]]);
  $('#resultados').text(arr);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id='procesar'>Mostrar Resultados</button>

<br/>
<br/>
<div id='resultados'>
</div>
    
answered by 31.01.2018 в 01:27