Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda