Eric Scalise
Eric Scalise

Reputation: 5

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.

Upvotes: 0

Views: 242

Answers (2)

Tibrogargan
Tibrogargan

Reputation: 4603

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

Upvotes: 0

Siva Kondapi Venkata
Siva Kondapi Venkata

Reputation: 11001

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

Upvotes: 1

Related Questions