Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
Filter some values of big array in Javascript

I have an array fetched from our server which holds 2400 objects (total size is about 7MB) and I want to filter some first values in it. Right now I'm using combination of filter and slice method:

 const keyword = 'whatever word';
 const recommendList =bigArray.filter(item => item.name.includes(keyword)).slice(0, 5);

What I know is filter method iterates all the element in array and I think it can impact to performance of my app (React Native) cause its large data. So is there any approach to filter the array for some values, without iterating all the elements ?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

const keyword = 'whatever word';
const recommendList = [];
for (let i=0; i<bigArray.length; i++) {
  if ( recommendList.length >= 5) 
    break;

  const item = bigArray[i];
  if (item.name.includes(keyword)) 
    recommendList.push(item);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

If you simply want to to break(stop) the loop when you find 5th element then you can do the bellow:

const keyword = 'v';
const bigArray = ['a','v','a','v','a','v','a','v','a','v','a','v','a','v','a','v','a','v'];
const recommendList = [];
for (let i=0; i<bigArray.length; i++) { // loop till you reach end of big array index
  if (recommendList.length == 5) // if length is 5 this will break the loop
    { 
    break; 
    }
  
  if (bigArray[i].includes(keyword)) {
    recommendList.push(bigArray[i]); // add if you find 
    }
}
console.log(recommendList);

If you dont want to use lambda operation can simply use, some, find etc which only works till they return the first response as true

const bigArray = [{
    "name": "a"
  },
  {
    "name": "v"
  },
  {
    "name": "a"
  },
  {
    "name": "v"
  },
  {
    "name": "a"
  },
  {
    "name": "v"
  },
  {
    "name": "a"
  },
  {
    "name": "v"
  },
  {
    "name": "a"
  },
  {
    "name": "v"
  },
  {
    "name": "a"
  },
  {
    "name": "v"
  },
  {
    "name": "a"
  },
  {
    "name": "v"
  },
  {
    "name": "a"
  },
  {
    "name": "v"
  },
  {
    "name": "a"
  },
  {
    "name": "v"
  }
];

const keyword = 'v';
const recommendList = [];

// some operator only iterates till its condition returns true
// so if we get 5 recommended list before the bigArray end we return true and stop the iteration.
bigArray.some(obj => {
  if (obj.name.includes(keyword)) {
    recommendList.push(obj)
  }
  return recommendList.length === 5; // return true if 5 values are found, that will terminate the iteration
})


console.log(recommendList);

about 4 years ago · Juan Pablo Isaza Relatório

0

The most efficient approach is to process the array as an iterable sequence.

Example below is based on iter-ops library:

import {pipe, filter, take} from 'iter-ops';

const i = pipe(
    bigArray,
    filter(item => item.name.includes(keyword)),
    take(5)
);

console.log('matches:', [...i]);

This way, you won't be iterating through everything even once, it will stop just as the first 5 matches are found.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda