简体   繁体   中英

Filter an array based on values from another array with same length

I'm given an array of objects representing data about people. I need to filter names, which when add the ASCII representation of all characters in their first names, the result will be an odd number. eg: Sum of ASCII codes of letters in 'Aba' is: 65 + 98 + 97 = 260 which is an even number -- filter it out

My logic is as follows:

  1. get first names;
  2. convert each letter of the first name to ASCII code;
  3. add ASCII codes of each name;
  4. check, if the received number is odd;
  5. return names, which ASCII code number is odd.

I've completed 4 tasks of the above, but I do not know, how to connect items from different arrays. Eg: I have an array of names: let names = ['Aba', 'Abb'] , and array of odd/even numbers: let oddEven = [0, 1] .

How can I make:

1) names[0] is oddEven[0]

2) if oddEven[0] > 0 { filter it out } ?

Thanks

my code:

function findOddNames(list) {

  let names = list.map((name) => name.firstName);
  let splittedNames = names.map((name) => name.split(''));
  let charCodes = splittedNames.map((arr) => arr.map((letter) => letter.charCodeAt(0)));
  let charCodesTotal = charCodes.map((arr) => arr.reduce((a, b) => a + b, 0));
  let oddEven = charCodesTotal.map((item) => item % 2);    
};

findOddNames([
  { firstName: 'Aba', lastName: 'N.', country: 'Ghana', continent: 'Africa', age: 21, language: 'Python' },
  { firstName: 'Abb', lastName: 'O.', country: 'Israel', continent: 'Asia', age: 39, language: 'Java' }
])

You can use filter like this. oddEven has the modulo values. Just filter the indexes from names array which have non-zero oddEven values. To get even values, use oddEven[i] === 0

 function findOddNames(list) { let names = list.map((name) => name.firstName); let splittedNames = names.map((name) => name.split('')); let charCodes = splittedNames.map((arr) => arr.map((letter) => letter.charCodeAt(0))); let charCodesTotal = charCodes.map((arr) => arr.reduce((a, b) => a + b, 0)); let oddEven = charCodesTotal.map((item) => item % 2); let oddNames = names.filter((n, i) => oddEven[i] !== 0) return oddNames; }; const odd = findOddNames([ { firstName: 'Aba', lastName: 'N.', country: 'Ghana', continent: 'Africa', age: 21, language: 'Python' }, { firstName: 'Abb', lastName: 'O.', country: 'Israel', continent: 'Asia', age: 39, language: 'Java' } ]) console.log(odd)

This alternative uses the function filter to get only odd names and the function reduce to sum the ASCII code for each letter, and finally the operator % to check if the sum is even or odd.

 let findOddNames = (arr) => { return arr.filter(({firstName}) => { return firstName.split('').reduce((a, letter) => letter.charCodeAt(0) + a, 0) % 2; }); }; let result = findOddNames([ { firstName: 'Aba', lastName: 'N.', country: 'Ghana', continent: 'Africa', age: 21, language: 'Python' }, { firstName: 'Abb', lastName: 'O.', country: 'Israel', continent: 'Asia', age: 39, language: 'Java' } ]); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

Why not use .filter instead without creating separate arrays to hold each variation of your input array.

Below, I have used .filter to filter out your input array into your findOddNames array. If you want to keep a given element when using filter, you need to return true , if you don't want to keep it (ie sum of the firstName is even) then you can return false .

In the filter function, I have also made use of destructuring assignment , which allows me to easily pull out the firstName property from your input array. I have also used the spread syntax . This is an easy way to convert a string of characters, into an array of characters. Finally, I used .reduce on the array to get the sum of all the characters in the array.

See example below:

 const findOddNames = arr => { return arr.filter(({firstName:n}) => { const sum = [...n].reduce((acc, letter) => acc + letter.charCodeAt(0), 0); return sum % 2 !== 0; }); } console.log(findOddNames([ { firstName: 'Aba', lastName: 'N.', country: 'Ghana', continent: 'Africa', age: 21, language: 'Python' }, { firstName: 'Abb', lastName: 'O.', country: 'Israel', continent: 'Asia', age: 39, language: 'Java' } ]));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM