Nested Arrays and Objects
Tags: javascript, array, object
Published: 2021-08-11 02:54:46

The job search continues and I was lucky enough to participate in two technical assessments. One of the assessments included parsing through some given data and converting it into an object. Within this data were three arrays, families, parents and children, that had to be converted into objects well. The last part of the function is the categories must be grouped by a given entry, such as id or name.

The first task was to take the given data and create a new Object from it to manipulate and configure the values. The data was already an object so it seemed pretty simple at first. It wasn’t until I started testing my solution that I began to notice that if you create a copy of the data and alter, this will also alter the original object. Obviously this wasn’t feasible for my solution when trying multiple test-cases.

At first, I thought using `Object.assign(data)` would render a brand new object that wouldn’t change the original object given. But because this was a deeply nested object, the assign function wouldn’t be strong enough to handle the conversion. Eventually I found a suggestion on StackOverflow that would be able to copy over the data into a separate Object, `JSON.parse(JSON.stringify(data))`.

Next, I had to navigate through the nested arrays and reformat each into a nested Object with a key defined by the name or id. The easiest solution seemed to take the array and iterate through each, then defining the necessary key-value pair.

```
families.forEach(function(family) {
    Brand.families[family.id] = family
}
```
For this example, the newly created Object would look like Brand: { Families: “F1” {id: “F1”, name: “Bundys”} }. Again, the function requires the identifying key to group each family by. To handle this static parameter, the array would be parsed through for the particular identifier by placing it square brackets as the key is being assigned.

The last part was considering any possible edge cases that could render the solution useless. My first thought was if the desired identifier wasn’t found in the data or in a particular array. An Object with a key of “undefined” would certainly not look good. Therefore I created a function that would take in an object and create a variable of the Object’s keys. 

Originally, I tried to iterate through the array of keys with forEach but the loop would continue even after the condition was met. Of course, the purpose of the function is right in the name, no matter if the condition was true or not. Like always, if forEach doesn’t work, I revert back to a simple `for...let` loop to iterate through the array. If the identifier isn’t found amongst the keys, then the function would return an id number in its place, depending on the place of the object given.

```
function identity(obj, search, place) {
 let etc = Object.keys(obj)
 for (let i = 0; i<etc.length;i++) {
   if (etc[i] == search) {
     return obj[search]
   } else {
     continue
   }
 }
 return `0${place+1}`
}
```

I’m still unsure if creating a new Object with an empty hash was the most efficient way to convert the nested arrays into nested Objects. I attempted to define the key-value pairs with the Object that was copied over from the data, but it would combine the existing arrays with the newly formatted Object data. 

Besides that, I was almost proud of my submission. It seemed clean and readable, and I thought my added function to check for the identifier was helpful. Hopefully I hear back soon and move onto the interview where I can really shine. Wish me luck!