0

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]]; 

    2 Answers 2

    5

    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])); 
    0
      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.

        Start asking to get answers

        Find the answer to your question by asking.

        Ask question

        Explore related questions

        See similar questions with these tags.