Iterate sur les objets dans l'objet, trouver les correspondances et remplacer

J'ai un objet retourné dans le format suivant:

[ { "category": "Coach", "phrase_original": "Training {{the team}} to develop their match skills by ensuring they are comfortable with {{defence techniques}}", "phrase_filter": "Training {{group}} to develop their {{attribute}} skills by ensuring they are comfortable with {{factor}}" }, { "category": "Coach", "phrase_original": "Assisting the {{fitness coach}} in strength and conditioning work to improve {{team performance}}", "phrase_filter": "Assisting the {{person}} in strength and conditioning work to improve {{factor}}" } ] 

Je voudrais analyser à travers chacun et remplacer:

  • {{group}} dans phrase_filter avec <span style="group*button">group</span>
  • {{attribute}} dans phrase_filter avec <span style="attribute-button">group</span>
  • {{factor}} dans phrase_filter avec <span style="factor-button">group</span>
  • {{person}} dans phrase_filter avec <span style="person-button">person</span>

Quelle serait la meilleure façon d'y parvenir?

C'est mon code jusqu'à présent, je ne suis pas sûr de mettre en œuvre ce qui précède. Je reçois actuellement les données du point d'extrémité de l'API, mais je ne sais pas trop comment la traiter.

 CategoryService.getCategoryDetail($scope.categoryId).then(function(dataResponse) { $scope.categoryDetail = dataResponse.data; angular.forEach($scope.categoryDetail, function(e) { // 1. find all the words in braces in phrase_filter // 2. replace them with html markup so they are rendered in the view differently // e.phrase_filter = ??? }); }); 

Vous pouvez essayer quelque chose comme ça:

 var inputs = [ { "category": "Coach", "phrase_original": "Training {{the team}} to develop their match skills by ensuring they are comfortable with {{defence techniques}}", "phrase_filter": "Training {{group}} to develop their {{attribute}} skills by ensuring they are comfortable with {{factor}}" }, { "category": "Coach", "phrase_original": "Assisting the {{fitness coach}} in strength and conditioning work to improve {{team performance}}", "phrase_filter": "Assisting the {{person}} in strength and conditioning work to improve {{factor}}" } ] var replacers = { '{{group}}' : '<span style="group-button">group</span>', '{{attribute}}' : '<span style="attribute-button">attribute</span>', '{{factor}}' : '<span style="factor-button">factor</span>', '{{person}}' : '<span style="person-button">person</span>' } // Loop through every objects inputs.forEach(function(input){ Object.keys(replacers).forEach(function(key){ // Replace every occurence of this key input['phrase_filter'] = input['phrase_filter'].replace(new RegExp(key, 'g'), replacers[key]); }) }) console.log(inputs);