1

I am having troubles converting an array into an object of keys and values.

Currently, I have the following nested array:

var array = [[id1, parent1, "name1", "desc1"], [id2, parent1, "name2", "desc2"], [id3, parent1, "name3", "desc3"]];

Where the length of the array is dynamic.

For my code, I require the array to be converted such that it is an object of keys and values (consisting of the first (id) and third (name) value of each nested array).

For example, the object for the above array would be as follows:

var obj = {id1: name1, id2: name2, id3: name3};

Where the id values (id1, id2, id3) would be the corresponding integer values.

I apologise if a similar question has been asked before, but I couldn't seem to find a similar question which had a solution that worked for me.

Any help or advice would be greatly appreciated!

adamcampbell
  • 53
  • 1
  • 8
  • @Barmar apologies, I had no intentions of bothering anyone. In future, I will be more clear of what I have tried already, and I will reference the "How to Ask" thread which you have brought to my attention. – adamcampbell Oct 22 '20 at 00:31

4 Answers4

1

You basically want to convert your original array into an array of [key, value] pairs. You can then use the Object.fromEntries function to convert those key/values into an object. So, something like this:

const arr = [
  ["id1", "parent1", "name1", "desc1"],
  ["id2", "parent2", "name2", "desc2"],
  ["id3", "parent3", "name3", "desc3"],
];

const results = Object.fromEntries(arr.map(x => ([x[0], x[2]])))

console.log(results)
Cully
  • 6,427
  • 4
  • 36
  • 58
1

You can use a simple for loop to do it

var array = [
  ["id1", "parent1", "name1", "desc1"],
  ["id2", "parent1", "name2", "desc2"],
  ["id3", "parent1", "name3", "desc3"]
];

const obj = {}
for (const item of array) {
  obj[item[0]] = item[2];
}

console.log(obj);
IvanD
  • 2,728
  • 14
  • 26
1

After using Array.map to extract the first and third entries from each element in the array, you can then use Object.fromEntries to convert the extracted array of key/value pairs into an object:

const [id1, id2, id3, parent1] = [1, 2, 3, 4];

const array = [
  [id1, parent1, "name1", "desc1"],
  [id2, parent1, "name2", "desc2"],
  [id3, parent1, "name3", "desc3"]
];

const obj = Object.fromEntries(array.map(a => [a[0], a[2]]));
console.log(obj);
Nick
  • 138,499
  • 22
  • 57
  • 95
0

As a good practice, it is recommended to use let or const instead of var because var "pollute" the global namespace, so that's what my example will use.
However, if you need to use var, you can replace const with var inside my example and it will still be okay.

Given the following source array:

const array = [
  [id1, parent1, "name1", "desc1"],
  [id2, parent1, "name2", "desc2"],
  [id3, parent1, "name3", "desc3"]
];

The following block of code create an object named obj using the 1st element of the sub-array as a key, and the 3rd element as the value:

// Create an empty object
const obj = {};

// Iterate through the source array
array.forEach((element) => {
  // Assign the 1st element of the sub-array as the property key
  // and the 3rd element as the property value
  obj[element[0]] = element[2];
});

console.log(obj);

And this has the same effect, but simpler and with a smaller footprint:

const obj = Object.fromEntries(array.map(([key, _, value]) => [key, value]));

console.log(obj);
Paul-Louis Mas
  • 429
  • 3
  • 8