a line of my object array is missing

Asked

Viewed 56 times

1

I’m trying to run this algorithm js in order to exercise what I’ve learned, the idea was to multiply the ages by 2 and then create a filter that would remove users under the age of 50, but the algorithm find that should go through the whole array only executes the first iteration that the condition is accepted, I do not know what the error of the algorithm

//script

const usuarios = [
    {nome: 'diego', idade: 23, empresa: 'rocketseat'},
    {nome: 'gabriel', idade: 15, empresa: 'rocketseat'},
    {nome: 'lucas', idade: 30, empresa: 'facebook'}
];



usuarios.map(function(item){ item.idade *= 2});

const novosUsers = usuarios.find(item => item.idade <= 50);

console.log(novosUsers);

3 answers

3

No item is missing from your vector, it turns out you used the method find, that returns the first item that meets your condition:

const usuarios = [
    {nome: 'diego', idade: 23, empresa: 'rocketseat'},
    {nome: 'gabriel', idade: 15, empresa: 'rocketseat'},
    {nome: 'lucas', idade: 30, empresa: 'facebook'}
];

usuarios.map(function(item){ item.idade *= 2});

const novosUsers = usuarios.find(item => item.idade <= 50);

console.log(novosUsers);

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find


If you want to return all the items of your vector that meet your condition, you must use the method filter:

const usuarios = [
    {nome: 'diego', idade: 23, empresa: 'rocketseat'},
    {nome: 'gabriel', idade: 15, empresa: 'rocketseat'},
    {nome: 'lucas', idade: 30, empresa: 'facebook'}
];

usuarios.map(function(item){ item.idade *= 2});

const novosUsers = usuarios.filter(item => item.idade <= 50);

console.log(novosUsers);

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filtro

  • Thank you, my mistake of attention.

1

Actually the "map" function does not modify the list, it returns another modified list. Also, you are using the "find" method instead of "filter" to filter the elements. Below is a modified version of your code that may be what you are looking for:

const usuarios = [
{nome: 'diego', idade: 23, empresa: 'rocketseat'},
{nome: 'gabriel', idade: 15, empresa: 'rocketseat'},
{nome: 'lucas', idade: 30, empresa: 'facebook'}
];

let usuariosComIdadeDuplicada = usuarios.map(function(item){ return {...item, idade:  item.idade * 2}; });

const novosUsers = usuariosComIdadeDuplicada.filter(item => item.idade <= 50);

console.log(novosUsers);

0

The algorithm is working correctly.
The Array.prototype.find() it traverses the array and returns the first element it has true as a result of callback.
To return everyone who passes on condition, you use the Array.prototype.filter(). This yes will traverse and return a new array with the elements that passed in the callback condition.

  • thanks, I had n noticed that I used the wrong function.

Browser other questions tagged

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