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

137
Views
Looping through an array of arrays and removing an item

My question is quite odd because it's for OfficeScripts. I am pretty sure it is Javascript-based because that is what I have been writing in. Now onto my question. I have an array of arrays that I am backward looping through in order to remove some items. basically, I gathered row data from one table and then used it to filter other tables, and then built an array of an array using both data. looking something like this:

const Data = [["Name", "Area", "Position", "Shift"]];
const otherData = [["Training A","Course A","Level","Curricula"],["Training B","Course B","Level","Curricula"],["Training C","Course C","Level","Curricula"]];
var CombinedData = otherData.map(x=>[...Data[0], ...x]);
console.log(CombinedData);

What I am looking to accomplish is to remove the "Position" from every array in the CombinedData array of arrays. Right now I am writing this and it's emptying my array:

const Data = [["Name", "Area", "Position", "Shift"]];
const otherData = [["Training A","Course A","Level","Curricula"],["Training B","Course B","Level","Curricula"],["Training C","Course C","Level","Curricula"]];
    var CombinedData = otherData.map(x=>[...Data[0], ...x]);
    console.log(CombinedData);
    
    for(let i = CombinedData.length -1; i>=0; i--){
      if(CombinedData[i][2] == Data[0][2]){
        CombinedData[i].splice(i,1);
          
      }
    }
    console.log(CombinedData);
  
    
What I would like the end output to look like is

CombinedData= [["Name", "Area", "Shift","Training A","Course A","Level","Curricula"],["Name", "Area", "Shift","Training B","Course B","Level","Curricula"],["Name", "Area", "Shift","Training C","Course C","Level","Curricula"]];

Which obviously isn't working. Any thoughts on what I need to do? JavaScript isn't my strongest language, and I am completely new to OfficeScripts. I appreciate any help.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Issue is with splice(i, 1), This should be splice(2, 1) since you need to remove the 2 (indexed) item.

const Data = [["Name", "Area", "Position", "Shift",]];
const otherData = [["Training A","Course A","Level","Curricula"],["Training B","Course B","Level","Curricula"],["Training C","Course C","Level","Curricula"]];
    var CombinedData = otherData.map(x=>[...Data[0], ...x]);
    
    for(let i = CombinedData.length -1; i>=0; i--){
      if(CombinedData[i][2] == Data[0][2]){
        CombinedData[i].splice(2,1);
          
      }
    }
    console.log(CombinedData);

about 4 years ago · Juan Pablo Isaza Report

0

Since you only ever use element 0 of Data, just filter that once and use that in your combination:

const Data = [["Name", "Area", "Position", "Shift",]];
const otherData = [["Training A","Course A","Level","Curricula"],["Training B","Course B","Level","Curricula"],["Training C","Course C","Level","Curricula"]];
var elem0 = Data[0].filter( y => y != "Position" )
var CombinedData = otherData.map(x=>[...elem0, ...x]);
console.log(CombinedData);

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!