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

121
Visualizações
Copying selected data from one array to another in javascript

Trying to grab certain information from a larger array to a newer array. Is there a more efficient way I can parse out the data I want using only the indices from the desire_contents

airport_data_parser() does work but I am seeking a that can have the same output but using desire_contents

airport_data = [["JFK","New York","United States",1,true],
                ["BOS","Massachusetts","United States",2,false],
                ["LOS","Califonia","United States",3,true],
                ["SJC","San Juan","United States",4,false]];
                
currated_airport_data = []; 

//desire_contents is an array that contains the values of desired indices I want
desire_contents = [0,1,3];

unwanted_contents = [2,3];//tried [2,4] but for some reason [2,3] works

var airport_data_parser = function () { 
    if(airport_data.length > 0) {
        for(var data in airport_data) {
            currated_airport_data.push(airport_data[data]);
            for(contents in unwanted_contents) {
                currated_airport_data[data].splice(unwanted_contents[contents], 1);
            }
        }
    }
    console.log(currated_airport_data);
}

airport_data_parser(); 

Desired Result

currated_airport_data = [["JFK","New York",1],
                    ["BOS","Massachusetts",2],
                        ["LOS","Califonia",3],
                         ["SJC","San Juan",4]];
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try this:

currated_airport_data = airport_data.map(arr => [arr[0], arr[1], arr[3]]);

Or the more generalized solution:

indexes = [0,1,3];
currated_airport_data = airport_data.map(arr => indexes.map(ind => arr[ind]));
about 4 years ago · Juan Pablo Isaza Relatório

0

var desire_contents = [0,1,3];

var currated_airport_data= airport_data .map(
                      x=>x.filter(
                                  (x,index)=>desire_contents.includes(index)
                                 )
                                     );`

You can select your data with using map and filter functions. In map function, we get the every single airport arrays one by one and then, filter it. In filter function we checked if index is existing in our desired array. If it existing in our desired array we keep it in list. Otherwise, filter function doesn't appends that value to the new array.

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