0

The view I have is updated when this.dataSource.data is updated. I want to push() new objects into a deeply nested object dynamically. I currently am feeding an array of viewValue names into a forEach() loop that first finds the index and, with this index, paths it's way down to the desired leaf node.

The issue I'm having is that I need to set this equal to this.dataSource.data, but as I'm digging into the nested object, I'm unable to keep the dot notation path.

I thought I could store the path as a string and convert it with eval() or something, but this seems sloppy, and I haven't gotten that to work either(it usually says that data in this.dataSource.data is undefined, which I'm not sure why. maybe it's a bug with the chrome debugger).

I have a feeling there is a simple answer, but my dumb eyes can't see it. I've looked for similar things on stack overflow, but haven't found anything that really is similar, even though there has to be, as this seems like a seemingly common sounding problem. Maybe, I'm just really bad at google searches.

EDIT: I don't think tracking the path via string and converting the string to an object path is the best approach. this would be a last resort, i think.

Edit2: made '.[results]' to '["results"]' removing the .

let pathingObject = this.dataSource.data
let pathingString = 'this.dataSource.data'
if (pathingArray === null) {
  this.serviceData.push(data)
  console.log(this.serviceData);
  this.dataSource.data = this.serviceData
} else {
  pathingArray.forEach(element => {
    let index = pathingObject.findIndex(p => p.viewValue == element);
    pathingObject = pathingObject[index]['results']
    pathingString = pathingString + '[' + index + ']' + '["results"]';
  });
  pathingObject.push(data);
  //console.log(eval(pathingString));
  // this.dataSource.data = pathingObject;
}
camelCase
  • 45
  • 6
  • Possible duplicate of [Convert JavaScript string in dot notation into an object reference](https://stackoverflow.com/questions/6393943/convert-javascript-string-in-dot-notation-into-an-object-reference) – VLAZ Aug 14 '19 at 13:59
  • I don't think converting to string and back is a good idea. it's more or less a fallback – camelCase Aug 14 '19 at 14:12
  • I'm not sure what you refer to, but that question deals with your exact situation - you have a string that describes the path to a value in an object and you want to use it to get to that value. There are multiple approaches and answers that do that. – VLAZ Aug 14 '19 at 14:15
  • Your problem seems to be elsewhere, first: you're not testing `index` to see if the element is found, unless you're sure that all elements in `pathingArray` are also in `pathingObject` entries's `viewValue` property, that will cause problems for following code. Second, you reuse the same variable here `pathingObject = pathingObject[index]['results']`, unless this is a recursive structure search, `pathingObject` will be replaced by the sub-value at `[index]['results']` (which will lead to `undefined` if `findIndex` found nothing). – Kaddath Aug 14 '19 at 14:18
  • Anyway, if this part of code is fine and previous comment is irrelevant, I suggest using an array of keys to keep your path instead of a string. You can simply loop on the array to access nested keys. – Kaddath Aug 14 '19 at 14:20
  • Yeah, the array is checked against a schema before this function is called. I think I see what you are saying, but I'd feel like i'd run into the same issue as I am here with the array of keys. I'd loop on the array of keys and either have the same pathingObject, as I wouldn't be able to concatenate pathingObject += [loopedKey].result. Am I misunderstanding you? – camelCase Aug 14 '19 at 14:29
  • Do you have a real reason you would want to keep a separated path? if all goes well in the `forEach`, `pathingObject` holds the reference to what you want and you don't need the path. Note that your path string is built incorrectly, if `index` is a string, it's missing the quotes, and you can't use dot with bracket -> use rather `pathingString + '["' + index + '"]' + '.results'` – Kaddath Aug 14 '19 at 14:37
  • I think I need the path, because I need to push this objects into ```this.dataSource.data[1].results[2].results array```. you are correct, I made a mistake with the ```.results```, but the ```'[' + index + ']'``` actually works fine. I'm looking at it in my debugger. – camelCase Aug 14 '19 at 14:43
  • No, either with brackets and quotes `+ '["results"]` either with dot `+ '.results'` – Kaddath Aug 14 '19 at 14:47
  • This isn't the issue that I'm having. I don't think that converting the path string is that sound of a strategy. I've updated the question to include your changes, however. – camelCase Aug 14 '19 at 14:52

0 Answers0