How to make select accept in the function query() in the result variable the result the filterIn() function and filter by its arguments 'name' , 'gender' , 'email' . I do not understand how to pass in select() these arguments 'name' , 'gender' , 'email' .

 var friends = [ { name: 'Бэм', gender: 'ΠœΡƒΠΆΡΠΊΠΎΠΉ', email: 'luisazamora@example.com', favoriteFruit: 'ΠšΠ°Ρ€Ρ‚ΠΎΡ„Π΅Π»ΡŒ' }, { name: 'Π­ΠΌΠΈΠ»ΠΈ', gender: 'ЖСнский', email: 'example@example.com', favoriteFruit: 'Π―Π±Π»ΠΎΠΊΠΎ' }, { name: 'ΠœΡΡ‚', gender: 'ΠœΡƒΠΆΡΠΊΠΎΠΉ', email: 'danamcgee@example.com', favoriteFruit: 'Π―Π±Π»ΠΎΠΊΠΎ' }, { name: 'Брэд', gender: 'ΠœΡƒΠΆΡΠΊΠΎΠΉ', email: 'newtonwilliams@example.com', favoriteFruit: 'Π‘Π°Π½Π°Π½' } ]; function query(collection) { var result = select(arguments[2]); return result; }; function select() { var selectArr = arguments[0]; for (var i = 0; i < friends.length; i++) { for (var key in friends[i]) { if (selectArr.indexOf.call(arguments, key) == -1) { delete selectArr[i][key]; } } } return selectArr }; function filterIn(property, values) { var arr = []; for (var q = 0; q < friends.length; q++) { for (var i = 0; i < values.length; i++) { if (friends[q].hasOwnProperty(property)) { if (friends[q][property] == values[i]) { arr.push(friends[q]) } } } } return arr }; var lib = query(friends, select('name', 'gender', 'email'), filterIn('favoriteFruit', ['Π―Π±Π»ΠΎΠΊΠΎ', 'ΠšΠ°Ρ€Ρ‚ΠΎΡ„Π΅Π»ΡŒ'])); console.log(lib) 

The result should output such.

 [ { name: 'Бэм', gender: 'ΠœΡƒΠΆΡΠΊΠΎΠΉ', email: 'luisazamora@example.com' }, { name: 'Π­ΠΌΠΈΠ»ΠΈ', gender: 'ЖСнский', email: 'example@example.com' }, { name: 'ΠœΡΡ‚', gender: 'ΠœΡƒΠΆΡΠΊΠΎΠΉ', email: 'danamcgee@example.com' }, { name: 'Π¨Π΅Ρ€Ρ€ΠΈ', gender: 'ЖСнский', email: 'danamcgee@example.com' }, { name: 'Π‘Ρ‚Π΅Π»Π»Π°', gender: 'ЖСнский', email: 'waltersguzman@example.com' } ] 
  • it would be better if you implemented all this in the form of a class with methods and not separate functions, then there will be no questions, as in one method you can use the results of another, because you can store intermediate results here - teran
  • Well, it was not me who did it, but the task and these three functions were given - Alexander Pupkin
  • Who are friends ? - Igor
  • Did anyone even understand the question? %) why are there arguments if the arguments are explicitly specified? - Daniel Khoroshko
  • if you need to filter an array of objects and select only the required fields, you can write this in 1 line in such a way that other programmers will understand in the future what the general meaning was - Daniel Khoroshko

1 answer 1

Judging by the description and example of the call, the function query should take three parameters, and not one, as it is now. And in general, it can be written as:

 function query(collection, selectFunc, filterFunc){ return collection.filter(filterFunc).map(selectFunc); } 

For this function it remains to obtain the necessary functions.

Let filterIn return a function that takes an object and returns true if the object satisfies the condition and false otherwise.

The condition can be written like this: the specified field must have one of the listed values

Then filterIn can take the following form:

 function filterIn(field, allowedValues){ return function filterFunc(element) { return allowedValues.includes(element[field]); }; } 

Now to the select function, you need to collect an object that includes only these fields by the fields you passed. This can be implemented using reduce

 function select(...fields){ return function selectFunc(element) { return fields.reduce((acc,fieldName)=>{ acc[fieldName] = element[fieldName]; return acc; },{}); }; } 

Example assembly:

 var friends = [{ name: 'Бэм', gender: 'ΠœΡƒΠΆΡΠΊΠΎΠΉ', email: 'luisazamora@example.com', favoriteFruit: 'ΠšΠ°Ρ€Ρ‚ΠΎΡ„Π΅Π»ΡŒ' }, { name: 'Π­ΠΌΠΈΠ»ΠΈ', gender: 'ЖСнский', email: 'example@example.com', favoriteFruit: 'Π―Π±Π»ΠΎΠΊΠΎ' }, { name: 'ΠœΡΡ‚', gender: 'ΠœΡƒΠΆΡΠΊΠΎΠΉ', email: 'danamcgee@example.com', favoriteFruit: 'Π―Π±Π»ΠΎΠΊΠΎ' }, { name: 'Брэд', gender: 'ΠœΡƒΠΆΡΠΊΠΎΠΉ', email: 'newtonwilliams@example.com', favoriteFruit: 'Π‘Π°Π½Π°Π½' } ]; function query(collection, selectFunc, filterFunc) { return collection.filter(filterFunc).map(selectFunc); } function filterIn(field, allowedValues) { return function filterFunc(element) { return allowedValues.includes(element[field]); }; } function select(...fields) { return function selectFunc(element) { return fields.reduce((acc, fieldName) => { acc[fieldName] = element[fieldName]; return acc; }, {}); }; } var lib = query(friends, select('name', 'gender', 'email'), filterIn('favoriteFruit', ['Π―Π±Π»ΠΎΠΊΠΎ', 'ΠšΠ°Ρ€Ρ‚ΠΎΡ„Π΅Π»ΡŒ'])); console.log(lib) 

  • Thank you very much for your reply) - Alexander Pupkin