Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!