遍历数组数组并删除一个项目
Looping through an array of arrays and removing an item
我的问题很奇怪,因为它是针对 OfficeScripts 的。我很确定它是基于 Javascript 的,因为那是我一直在写的内容。现在谈谈我的问题。我有一个数组数组,我正在向后循环以删除一些项目。基本上,我从一个 table 收集行数据,然后用它来过滤其他 table,然后使用这两个数据构建一个数组的数组。
看起来像这样:
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);
我想要完成的是从数组的 CombinedData 数组中的每个数组中删除“Position”。现在我正在写这个,它正在清空我的数组:
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);
我希望最终输出看起来像
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"]];
这显然不起作用。关于我需要做什么的任何想法? JavaScript 不是我最擅长的语言,而且我对 OfficeScripts 完全陌生。感谢您的帮助。
由于您只使用 Data
的元素 0,只需过滤一次并在您的组合中使用它:
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);
问题出在 splice(i, 1)
,这应该是 splice(2, 1)
,因为您需要删除第 2 个(索引)项目。
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);
我的问题很奇怪,因为它是针对 OfficeScripts 的。我很确定它是基于 Javascript 的,因为那是我一直在写的内容。现在谈谈我的问题。我有一个数组数组,我正在向后循环以删除一些项目。基本上,我从一个 table 收集行数据,然后用它来过滤其他 table,然后使用这两个数据构建一个数组的数组。 看起来像这样:
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);
我想要完成的是从数组的 CombinedData 数组中的每个数组中删除“Position”。现在我正在写这个,它正在清空我的数组:
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);
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"]];
这显然不起作用。关于我需要做什么的任何想法? JavaScript 不是我最擅长的语言,而且我对 OfficeScripts 完全陌生。感谢您的帮助。
由于您只使用 Data
的元素 0,只需过滤一次并在您的组合中使用它:
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);
问题出在 splice(i, 1)
,这应该是 splice(2, 1)
,因为您需要删除第 2 个(索引)项目。
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);