0

I'm currently getting data from a database of an array of objects. I need these objects to be reformatted to work with a UI package that we're using that requires these objects to look a specific way. The main array should look like this:

[{
   trim: 123,
   id: 123,
   children: [{
      trim: 123,
      id: 123,
   }],
}]

I'm successfully looping through the main array data and reformatting it into a new array using .map; however, I'm having issues doing the same thing but for the children key that's found in each object of the main array. Wondering if anyone could help me figure out the proper way to do this. I'll add my current code below and will be able to clarify more if needed.

if (this.$store.state.models.models) { // this is checking for the main array coming from the database
   const reformattedModels = this.$store.state.models.models.map((model) => {
   const tableModel = {
     trim: model.name,
     id: model.id,
     children: // Need to loop here to reformat array of objects that goes for the children key,
   };
   return tableModel;
 });
 return reformattedModels;

}

The array that I need to loop through for the children key is one of the keys found in the main array from the backend. It's called "trims" and looks like this:

[{
   name: 123,
   id: 123,
}]
2
  • What array do you want to map for your children property? Commented Apr 28, 2021 at 10:40
  • It's another array of objects that comes in the backend data. That array is called "trims" each element in that array has a name key and an id key Commented Apr 28, 2021 at 10:41

1 Answer 1

2

Create a reference to the function and call it recursively:

if (this.$store.state.models.models) {
  // store the function
  const mapper = model => ({
    trim: model.name,
    id: model.id,
    // use it for children, if there are any
    children: model.children?.map(mapper)
  });

  // use it for the models
  return this.$store.state.models.models.map(mapper);
}

I'm using optional chaining (?.) to call .map() only if model.children != undefined

Sign up to request clarification or add additional context in comments.

6 Comments

I'm not familiar with recursion, could you possibly give a little more detail? Or an example of what "mapper" would look like and also the loop for the children please?
That is literally the entire code. I've moved the arrow function out from the .map(model => ...) and stored it in const mapper = model => ... and the "loop for the children" is here: children: model.children?.map(mapper) using optional chaining (?.) to call .map() only if model.children != undefined
Ahh okay, I see now. The only thing is, what happens if in the future the children objects need keys that aren't found in the reference? How would the new keys be included then?
Exactly like the current keys. It's up to you how you "compute" the value that you assign to a key, it doesn't have to come from the model.
Okay, I tried it and it seems to work until it gets to the loop for the children; it says: "Error: for nested data item, row-key is required." Would you know the reason for this error? If not, I'll figure it out so no worries. It's probably a UI package specific thing
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.