D
D
denis_diz2021-07-11 00:13:31
JavaScript
denis_diz, 2021-07-11 00:13:31

How to rewrite the code to get the result without using a loop?

Good day, tell me how you can rewrite this code to get the same result without using a loop (maybe through map or forEach)?
(data is data from the server in the form of an array with objects)

const offers = [];
 let _i = 0;
 let result;

  while (_i < data.length && offers.length < 10) {
    result = filters.every((filter) => (filter.value === DEFAULT_VALUE) ? true : FilterRules[filter.id](data[_i], filter));
    if (result) {
      offers.push(data[_i]);
    }
    _i++;
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2021-07-11
@denis_diz

const offers = [];

data.every(item=>{
    const result = filters.every(filter => filter.value === DEFAULT_VALUE ? true : FilterRules[filter.id](item, filter));
    if (result) offers.push(item);
    return offers.length < 10;

})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question