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

Asked

Viewed 1,013 times

-1

I have an object JSON of a form, then converted normally and created a map only with the names of users within a array. The next step I need to take is to analyze all the names and get the(s) that has the biggest length.

I’ve tried to use filter and reduce, but there’s always some kind of mistake, someone could tell me which algorithm to use?

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

The result should be an array with the larger(s) name(s length.

  • sure understood your doubt, however from which length you want to assign to this new array of names?

  • If an answer solves your problem mark it as accepted. See how in https://i.stack.Imgur.com/jx7Ts.png and why in https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

2 answers

4

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

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 matrix, return an array of them. If there is only one with maior length, will return 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 one 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));

  • Thank you very much for your contribution <3

2

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

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);
  • Thank you very much for your contribution <3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.