Javascript command

Asked

Viewed 86 times

-2

I have a question, I need to create a function that receives a Array of people objects, which returns a new array only with person objects that are between the ages of 20 and 30.

Can someone help me if possible?


function pessoa(objec) {
    var olders = objec.filter(function(person){
        return person.age >= 20 && <= 30;
    });
    return olders;
}

It’s right that way?

  • 1

    Welcome to Stackoverflow in English. A good practice to start a healthy discussion is to read the Guide to How to Ask. Start by following these recommendations, especially knowing what types of questions to ask, how to create a minimal example that is complete and verifiable, and even what to do when someone answers you.

  • The language itself has a function for this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

  • What have you done about it? Edit your question and put the code of what you did to get better help

  • Function pessoa(objec) { var olders = objec.filter(Function(person){ Return person.age >= 20 && <= 30; }); Return olders; }

1 answer

1

Using the method filter

var pessoas = [{nome: 'Sérgio', idade: 35},
               {nome: 'Jéferson', idade: 25},
               {nome: 'Joaquim', idade: 19}];

var adultos = filtrarPessoas(pessoas);
console.log(adultos);

function filtrarPessoas(pessoas){
  var filtrados = pessoas.filter(function (item) {
    return item.idade >= 20 && item.idade <= 30;
  });
  
  return filtrados;
}

  • Function pessoa(objec) { var olders = objec.filter(Function(person){ Return person.age >= 20 && <= 30; }); Return olders; } that command is wrong ?

  • No. That’s right, because?

  • that’s my doubt if it’s right. I’m still running but I’m in doubt in return.

Browser other questions tagged

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