0

I'd like to pass in a string from one of my functions that will let another function know which array to update within another array. All the attempts I've tried end in poor errors that really don't lead me anywhere productive so far.

function likeFeedItem(itemId, feedItemIndex) {
  postActionsService.likeFeedItem(itemId)
    .then(function success(response) {
      var resource = 'capabilities.chatterLikes';
      ctrl.testUpdate(response.data, feedItemIndex, resource);
    })
    .catch(ctrl.showError)
};

function testUpdate(feedItemData, feedItemIndex, resource) {
  ctrl.feedResult.elements[feedItemIndex] . resource = feedItemData.resource;
  // Output should work/look like this would
  ctrl.feedResult.elements[feedItemIndex].capabilities.chatterLikes = feedItemData.capabilities.chatterLikes
}
2
  • If you are making use of AngularJS, I don't see any controller or services there. Please show the running code so that one will be able to understand better. Commented Feb 3, 2016 at 4:26
  • @Shashank turned out not to be relevant, thanks anyways! Commented Feb 3, 2016 at 4:35

1 Answer 1

1

Change:

ctrl.feedResult.elements[feedItemIndex] . resource = feedItemData.resource;

To:

resource = resource.split('.');
ctrl.feedResult.elements[feedItemIndex][resource[0]][resource[1]] = feedItemData[resource[0]][resource[1]];

EDIT: Also removed AngularJS tag as this doesn't have anything to do with it specifically.

EDIT2:

If you have an unknown amount of keys, you can loop like so:

resource = resource.split('.');
var current_node = ctrl.feedResult.elements[feedItemIndex];
for (var i = 0, n = resource.length; i < n; i++) {
    current_node = current_node[resource[i]];
}
Sign up to request clarification or add additional context in comments.

7 Comments

Awesome, thanks! Brand new the angular so wasn't sure. Added the same resource to the other side of your answer since it needs to be there as well.
I approved the edit. I didn't know if resource was actually a key for feedItemData or the same one, haha, so I left that one alone.
Fair enough, that makes sense. Thank you again for your help
any recommendations for if the argument (resource) is an unknown length? It could be capabilities.chatterLikes or could be capabilities.comments.page.items?
I've edited my answer to provide an example using a for loop.
|

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.