英文:
Convert 2 Dimensional Array into Object
问题
我希望能帮助将一个二维数组转换为对象。
在我的电子表格中,我有一个包含名称和Google Drive文件夹标识符的表格(这些只是示例,不是真正的标识符哈哈) -
名称 | 标识符 |
---|---|
名称1 | 27uXqJux5ub4xlSOJpDgozuSBXpzdaqMbz |
名称2 | VDe67puraYNFyPNCptBkffuQcajjJLFzE |
使用<kbd>getvalues()</kbd>,我已经创建了一个包含这些单元格的数组,称为<kbd>folderIds</kbd>(不包括标题行)。
然后,我有另一个只包含名称的数组,称为<kbd>names</kbd>。
[名称1,名称2,名称3,等等]
我的意图是运行一个循环,该循环使用第二个数组中的名称来检索第一个数组中的文件夹标识符,但我不知道如何将<kbd>folderIds</kbd>转换为对象,以便我可以使用<kbd>names</kbd>数组作为键来检索文件夹标识符。解决方案需要是动态的,因为<kbd>folderIds</kbd>将会随着时间的推移而增长(可能会更改),所以我不能手动定义对象。
理想情况下,我希望有一个对象,看起来像这样 -
const folderIdsObj = {
"名称1" : "27uXqJux5ub4xlSOJpDgozuSBXpzdaqMbz",
"名称2" : "VDe67puraYNFyPNCptBkffuQcajjJLFzE"
等等
}
然后,我会在循环中使用<kbd>folderIdsObj[names[i]]</kbd>来检索相关的文件夹标识符。
我已经在互联网上搜索了很多,看起来解决方案可能是使用map方法,但我以前从未使用过map,也不太确定从哪里开始。我尝试过,但无法弄清楚如何将folderIds[0][0]映射到folderIds[0][1](例如)。
也许我错过了一个更简单的解决方案,尽管我尝试了各种数组方法都没有成功。
提前感谢您的帮助!
英文:
I'm hoping for some help converting a 2D array into an object.
In my spreadsheet I have a table of names and Google Drive folder identifiers (these are just examples, not real identifiers haha) -
Name | Identifier |
---|---|
Name1 | 27uXqJux5ub4xlSOJpDgozuSBXpzdaqMbz |
Name2 | VDe67puraYNFyPNCptBkffuQcajjJLFzE |
Using <kbd>getvalues()</kbd>, I have created an array of these cells called <kbd>folderIds</kbd> (leaving out the header row).
I then have another array which contains just the names, called <kbd>names</kbd>.
[Name1, Name2, Name3, etc]
My intention is to run a loop which uses the names in the second array to retrieve the folder identifier in the first array, but I'm stuck on how to convert <kbd>folderIds</kbd> into an object so that I can use the <kbd>names</kbd> array as a key to retrieve the folder identifier. The solution needs to be dynamic as <kbd>folderIds</kbd> will grow (and possibly change) over time, so I can't just manually define the object.
Ideally I would have an object that looks like this -
const folderIdsObj = {
"Name1" : "27uXqJux5ub4xlSOJpDgozuSBXpzdaqMbz",
"Name2" : "VDe67puraYNFyPNCptBkffuQcajjJLFzE"
etc
}
And I would use <kbd>folderIdsObj[names[i]]</kbd> in a loop to retrieve the relevant folder identifier.
I've been digging around all over the internet, and it looks like the solution might be to use the map method, but I've never used map before and I'm not really sure where to start. I had a go but couldn't figure out how to map folderIds[0][0] to folderIds[0][1] (for example).
There may be a much simpler solution that I've missed too, though I've tried various array methods with no success.
Thanks in advance!
答案1
得分: 0
你可以通过实现这段代码来实现这一点...
twoDArray = [["Name1", "12ee1du912nc1"], ["Name2", "1231cqnisani83"]]
const obj = Object.fromEntries(twoDArray.map(([k, v]) => [k, v]));
console.log(obj);
更多信息请参考这里。
英文:
you can achieve this by implementing this code...
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
twoDArray = [["Name1","12ee1du912nc1"], ["Name2", "1231cqnisani83"]]
const obj = Object.fromEntries(twoDArray.map(([k, v]) => [k, v]));
console.log(obj);
<!-- end snippet -->
more information here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论