0

I would like to transform property of my object: lists_to_download: { 10920: "10920" } to lists_to_download: ["10920"], so I've created a processor class, which handles this type of operation, but it doesn't mutate my input object and I don't know, where's the problem. Take a look at the code bellow and see the console.log outputs there.

class ObjectKeysToArraySettingsProcessor { constructor(objectPath = []) { this.objectPath = objectPath } _preProcessRecursion(part, objectPathIndex) { if(_isArray(part) && objectPathIndex < this.objectPath.length - 1) { part.forEach(item => { this._preProcessRecursion(item, objectPathIndex + 1) }) } else if(objectPathIndex === this.objectPath.length - 1) { if(_isEmpty(part)) { part = [] } else { console.log(part) // it prints { 10920: "10920" } part = _map(part, id => id) console.log(part) // it prints ["10920"] } } else { this._preProcessRecursion( part[this.objectPath[objectPathIndex + 1]], objectPathIndex + 1 ) } } preProcessSettings(settings) { if(!_isEmpty(this.objectPath)) { try { this._preProcessRecursion( settings[this.objectPath[0]], 0 ) } catch(err) { console.log(err) return settings } } console.log(settings) // but here it prints lists_to_downloads: { 10920: "10920" } again... return settings } } 
5
  • Probably _map(part, id => id) creates a new array and you are assigning it to a local variable path. But the reference to the old value of path is kept in settings object. In order to do what do you want you have to pass settings to the function and change the value in settings object. Commented Sep 25, 2018 at 9:28
  • @RidgeA I can't pass settings to the function, I have to do it recursively, because object path can be fex. ['parameters', 'films', 'additional' 'lists_to_downloads'], where 'films' could be an array. Also I taught that objects are passed by reference. Commented Sep 25, 2018 at 9:35
  • 1
    But you anyway can't do it in the way you want to. JS doesn't work like this. You have either to pass an object that will hold a property with reference to another object or return a new value from the function and reassign it outside (in preProcessSettings function according to your example) Commented Sep 25, 2018 at 9:41
  • @RidgeA thx, I'll probably know, what you mean. Commented Sep 25, 2018 at 9:42
  • const newKeys = Object.keys(data).map( key => data[key]) Commented Sep 25, 2018 at 10:05

1 Answer 1

1

Solution was to do the transformation in previous / not last step of the recursion

_preProcessRecursion(part, objectPathIndex) { if(_isArray(part) && objectPathIndex < this.objectPath.length - 1) { part.forEach(item => { this._preProcessRecursion(item, objectPathIndex + 1) }) } else if(objectPathIndex === this.objectPath.length - 2) { const attribute = this.objectPath[objectPathIndex + 1] if(_isEmpty(part[attribute])) { part[attribute] = [] return } else { part[attribute] = _map( part[attribute], id => id ) } } else { this._preProcessRecursion( part[this.objectPath[objectPathIndex + 1]], objectPathIndex + 1 ) } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.