Filter an Array of names and return the largest name (s)

-1

I am with a JSON object of a form, so I converted it normally and created a map with only the names of users within a array . The next step I need to do is parse all the names and get the one (s) that has the highest length .

I've tried to use filter and reduce , but it always gives some kind of error, could someone tell me which algorithm to use?

Const nomes =["Luan","Pedro","Caio", "Mariana", "Lígia", "Rafaela"]

The result must be an array with the name (s) of the highest% of%.

    
asked by anonymous 12.12.2018 / 23:06

2 answers

4

We find the longest name of an array item with the reduce function, and then filter the array with elements that have that length with the filter function. It returns several elements if they have the same length as the

const nomes =["Luan","Pedro","Caio", "Mariana", "Lígia", "Rafaela"];

ln = nomes.reduce((r,s) => r > s.length ? r : s.length, 0);

const result = nomes.filter(pl => pl.length == ln);

console.log(result);

If there is more than one string of maior length in the array, it returns an array of them. If there is only one with maior length , it returns the string and not an array.

1 - example with a name of maior length

const nomes = ["Luan","Pedro","Caio", "Mariana", "Lígia", "Rafaela", "Andrei Coelho"];
var todosArr = [];

function stringsMaisCompridas(arr) {
    var tlength = 0;
    for(var i =0; i < nomes.length; i++){
      if(tlength < nomes[i].length){
        tlength = nomes[i].length;
      }
    }
    for(var j =0; j < nomes.length; j++){
      if(nomes[j].length == tlength){
         todosArr.push(nomes[j]);
      }
    }
   if(todosArr.length == 1){
     return todosArr[0]
   }else{
      return todosArr
  }
}


console.log(stringsMaisCompridas(nomes));

2 - example with more than maior length

const nomes = ["Luan","Pedro","Caio", "Mariana", "Lígia", "Rafaela", "Andrei Coelho", "Fulano Santos", "Ciclano Pedra"];
var todosArr = [];

function stringsMaisCompridas(arr) {
    var tlength = 0;
    for(var i =0; i < nomes.length; i++){
      if(tlength < nomes[i].length){
        tlength = nomes[i].length;
      }
    }
    for(var j =0; j < nomes.length; j++){
      if(nomes[j].length == tlength){
         todosArr.push(nomes[j]);
      }
    }
   if(todosArr.length == 1){
     return todosArr[0]
   }else{
      return todosArr
  }
}


console.log(stringsMaisCompridas(nomes));
    
13.12.2018 / 00:02
2

Well, first I get the array of names in the variable arrayOne , then I get the first item of that array and assign its length to a variable, nameLength .   Then I made a map in my arrayOne and I see the names that have the size bigger than my variable nameLength I give a push in the new array arrayTwo which is the array of the biggest names. And if the length of the name is smaller I just give a console warning.

var arrayOne = ["Luan","Pedro","Caio", "Mariana", "Lígia", "Rafaela"];
var nameLength = arrayOne[0].length;
var arrayTwo = [];
arrayOne.map(name =>{
    name.length > 4 ? arrayTwo.push(name) : console.log("length menor") ;
})

console.log(arrayTwo);
    
12.12.2018 / 23:29