1

I need to make a function that receives 2 parameters, the first one is an array of a list of users that must contain first name, last name and phone numbers, at least 3 of those users must start with the letter "a", the second parameter is a callback. You must process the array and delete all the users whose name starts with the letter "a". Then send the new processed array to the callback. The callback must show in console, the list of all the names, concatenating first and last name.

const users =
    [{
        name: 'Diego',
        lastname: 'Garcia',
        phone: '12343'
    },
    {
        name: 'Camilo',
        lastname: 'Garcia',
        phone: '12343'
    }, {
        name: 'ana',
        lastname: 'Rodriguez',
        phone: '02343'
    }, {
        name: 'anastasia',
        lastname: 'Zapata',
        phone: '42343'
    }, {
        name: 'alejandra',
        lastname: 'Perez',
        phone: '52343'
    }];


const operation2 = (list) => {
    let x = [];
    let callback = {};

    callback = list.find(element => element.name.charAt(0) == 'a');
    let l = list.indexOf(callback)

    let g = list[l];
    console.log(g)

    if (callback) {

        x = list.splice(l, 1)
    } return list

}

console.log(operation2(users))

The code that I made does not work, it is not eliminating all the names that begin with "a", it only eliminates the first object of the array.

  • Does this answer your question? [How to filter an array in javascript?](https://stackoverflow.com/questions/45916135/how-to-filter-an-array-in-javascript) – filipe Aug 27 '22 at 06:47

2 Answers2

2

Array.find only finds the first match, you should probably filter out the elements you don't want:

const result = list.filter(element => element.name.charAt(0) !== 'a');
filipe
  • 414
  • 1
  • 5
  • 14
0

const users =
    [{
        name: 'Diego',
        lastname: 'Garcia',
        phone: '12343'
    },
    {
        name: 'Camilo',
        lastname: 'Garcia',
        phone: '12343'
    }, {
        name: 'ana',
        lastname: 'Rodriguez',
        phone: '02343'
    }, {
        name: 'anastasia',
        lastname: 'Zapata',
        phone: '42343'
    }, {
        name: 'alejandra',
        lastname: 'Perez',
        phone: '52343'
    }];



function func(list, callback) {
  let users = list.filter(u => !u.name.toLowerCase().startsWith('a'));
  callback(users)
}

func(users, (users) => {
  console.log(users)
})
Mina
  • 14,386
  • 3
  • 13
  • 26