How to implement this code? How to make such functions? (this code cannot be changed)

  • friends - an array of objects
  • select, filterIn - functions that result in the work on the friends collection

Here's how friends put in the select and filterIn functions

var bestFriends = lib.query( friends, lib.select('name', 'gender', 'email'), lib.filterIn('favoriteFruit', ['Яблоко', 'Картофель']) ); 

Closed due to the fact that off-topic participants Darth , Air , 0xdb , Nicolas Chabanovsky 27 Feb '18 at 12:10 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • " Learning tasks are allowed as questions only on condition that you tried to solve them yourself before asking a question . Please edit the question and indicate what caused you difficulties in solving the problem. For example, give the code you wrote, trying to solve the problem "- Darth, Air, 0xdb, Nicolas Chabanovsky
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Try to reformulate the question. But it is not clear what is actually needed and with what difficulties? - Anton Shchyrov

1 answer 1

select, filterIn - functions that result in the work on the friends collection

Not certainly in that way. The .select() and .filterIn() functions are functions that will result in functions that will work on the friends collection.

The function .query() gets an input

  • friends collection
  • the function to select fields from the friends collection, obtained as a result of lib.select('name', 'gender', 'email')
  • filtering items from the friends collection from lib.filterIn('favoriteFruit', ['Яблоко', 'Картофель'])

Sample Code

 var lib = { query: function (data, select, filter) { return select(filter(data)) }, select: function (...fields) { return function (data) { return data.map(function (x) { var item = {} fields.forEach(function (field) { item[field] = x[field] }) return item }) } }, filterIn: function (field, values) { return function (data) { return data.filter(function (x) { return values.indexOf(x[field]) > -1 }) } } } var friends = [ { name: 'Михаил', gender: 'муж', email: 'mikhail@example.com', favoriteFruit: 'Тыква' }, { name: 'Алексей', gender: 'муж', email: 'aleksey@example.com', favoriteFruit: 'Дыня' }, { name: 'Федор', gender: 'муж', email: 'fedor@example.com', favoriteFruit: 'Яблоко' }, { name: 'Иван', gender: 'муж', email: 'ivan@example.com', favoriteFruit: 'Мандарин' }, { name: 'Петр', gender: 'муж', email: 'petr@example.com', favoriteFruit: 'Картофель' } ] var bestFriends = lib.query( friends, lib.select('name', 'gender', 'email'), lib.filterIn('favoriteFruit', ['Яблоко', 'Картофель']) ); console.log(bestFriends)