Convert 2 Dimensional Array into Object

Rosie Muir

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 getvalues(), I have created an array of these cells called folderIds (leaving out the header row).

I then have another array which contains just the names, called names.

[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 folderIds into an object so that I can use the names array as a key to retrieve the folder identifier. The solution needs to be dynamic as folderIds 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 folderIdsObj[names[i]] 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!

Carl Saqui

you can achieve this by implementing this code...

twoDArray = [["Name1","12ee1du912nc1"], ["Name2", "1231cqnisani83"]]
    
const obj = Object.fromEntries(twoDArray.map(([k, v]) => [k, v]));
    
console.log(obj);

more information here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

convert 3 dimensional array to a 2 dimensional array

Convert object to multi-dimensional array - JavaScript

How to convert multidimensional array into a 2 dimensional array?

convert array of tuples to 2 dimensional array

Pandas Data frame 2 dimensional array, to convert to a 3 dimensional array

How to convert 2 dimensional array to Set?

Convert 2-dimensional array to string in Javascript

convert a json 2 dimensional array into a table

Convert string to 2 dimensional numpy array

Storing object into 2 dimensional array/list

How to instantiate a class object with a 2 dimensional array?

2 dimensional array of managed object c++

Store object in 2 dimensional array in python?

How do I convert an array object into a two-dimensional array

How do you convert a List of object to a one dimensional array?

How to convert 2-dimensional double array from 2-dimensional Integer array in Java

Convert multi-dimensional Numpy array to 2-dimensional array based on color values

How can you convert a 2 dimensional array into a 1 dimensional array in Java

How can I conveniently convert a 2-dimensional array into a 2-dimensional vector?

Convert a Numpy 2 dimensional array to a mapped list of dictionaries

Convert JSONArray of double value to a 2 dimensional double array

How can I convert Pair to 2 dimensional array in JAVA?

How to convert 4 dimensional array into 2 dimensions and save as csv in python

Get 2-dimensional array of all arrays within an object

Create 2 Dimensional Array from Object with same key

Flutter 2 dimensional List/Array as Int/Object(class)

Convert 1 dimensional indices to 2 dimensional indices

convert 3 dimensional array into dataframe

Convert Multi dimensional array PHP