希望我能阐明这一点。
这个想法是我需要将多个Google文档合并为一个文档。我需要按位置执行此操作。
我有一张工作表,其中包含我放入对象数组(needBatches)中然后按条件(位置)分组的位置和文档ID:
const grouped = groupBy(needBatches, 'location');
function groupBy(objectArray, property) {
return objectArray.reduce(function (acc, obj) {
const key = obj[property];
if (!acc[key]) {
acc[key] = [];
}
acc[key].push(obj);
return acc;
}, {});
}
现在,我想获取存储在每个数组中的文档ID,因此我尝试执行一个forEach,但遇到类型错误:“在对象中找不到函数forEach”。
所以我这样迭代数组:
const arr = Object.keys(grouped).forEach(function(e) {
console.log(e, grouped[e])
});
现在,根据控制台日志,它看起来像这样:
Location1 [{documentLink1=<link>, documentId1=<id>, folder1=<folder>},{documentLink2=<link>, documentId2=<id>, folder2=<folder>}]
Location2 [{documentLink1=<link>, documentId1=<id>, folder1=<folder>},{documentLink2=<link>, documentId2=<id>, folder2=<folder>}]
它为我提供了每一行的所有内容,但实际上我只想要文档ID,但我不知道如何以这种格式调用它。
我尝试过:
grouped[e].documentId
grouped[e]["documentId"]
我刚刚开始使用对象数组,因此我的知识非常有限。谁能帮助我?
在这里,我仅输入没有文档的行:
const needBatches = dataRange.filter(function(e){
return e.documentLink !== ''
})
答案 0 :(得分:0)
如果数组和对象如我所了解,这就是我要迭代的方式:
var Location1 = [{
documentLink1: "Document 1 link1",
documentId1: "Document 1 id1",
folder1: "document 1 folder"
}, {
documentLink2: "Document 1 link2",
documentId2: "Document 1 id2",
folder2: "document 1 folder2"
}]
var Location2 = [{
documentLink1: "Document 2 link1",
documentId1: "Document 2 id1",
folder1: "document 2 folder"
}, {
documentLink2: "Document 2 link2",
documentId2: "Document 2 id2",
folder2: "document 2 folder2"
}]
var arrayLocation = [Location1, Location2];
arrayLocation.forEach((location, idx) => {
console.log(`Location number: ${idx}`)
console.log(`${location[0].documentLink1}`);
console.log(`${location[0].documentId1}`);
console.log(`${location[0].folder1}`);
console.log(`${location[1].documentLink2}`);
console.log(`${location[1].documentId2}`);
console.log(`${location[1].folder2}`);
})