0

Based on the result below , how can an angular for each loop be able to solve that json array of objects format ? The value is title and the id is key. Any Idea? Thank you.

mycode

me.record.questionaires = []
angular.forEach(detail.questionaires, function (value, key) {
            me.record.questionaires.push({ "id": key, "title": value })

        });

Formated json data (detail.questionaire result)

[  
   "{'sub_title': 'dsadsa', 'instruction': 'You Must',…elimit': '01:05:19', 'title': 'asdsa', 'id': 133}",
   "{'sub_title': 'sdasdsa', 'instruction': None, 'cre…melimit': '05:30:09', 'title': 'asda', 'id': 131}"
]

2 Answers 2

2

You need to

  1. Loop over the array
  2. Parse the string as JSON
  3. Push or map the appropriate values into your questionaires array (it's not clear what data you want)
me.record.questionaires = detail.questionaires.map(json => {
  let { id, title } = JSON.parse(json)
  return { id, title }
})
Sign up to request clarification or add additional context in comments.

2 Comments

nexpected token ' in JSON at position 1
@JhonCaylog well that's because your "formatted JSON data" is not JSON. Where does it come from? If you're manually typing it out, you should get the syntax correct (keys and string values must be quoted with double-quotes)
1

I had to change your sample formatted JSON a bit because it was giving me console errors. Please see if this helps.

angular
  .module("myModule", [])
  .controller("myController", function($scope) {
    var me ={record: {questionaires: []}};
    $scope.me = me;
	
    var detail ={};
    detail.questionaires = [  
      "{'sub_title': 'dsadsa', 'instruction': 'You Must','…elimit': '01:05:19', 'title': 'asdsa', id: 133}",
      '{"sub_title": "sdasdsa", "instruction": "None", "cre…melimit": "05:30:09", "title": "asda", "id": 131}'
    ];

    angular.forEach(detail.questionaires, function (value, key) {
    
      var questionaire = JSON.parse(value.replace(/'/g, '"').replace(/id:/g, '"id":'));     
      me.record.questionaires.push({ "id": questionaire.id, "title": questionaire.title });
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>

<div ng-app="myModule">
  <div ng-controller="myController">
  <div><strong>me.record.questionaires:</strong></div>
    <div ng-repeat="q in me.record.questionaires">
    <div>{{q}}</div>
    </div>
  </div>
</div>

12 Comments

The properties and values have to have double quotes instead of single quotes, and you had None without quotes... I am not sure if you meant to use null there instead or "None"
but that data is detail.questionaire
ang examplel ["{'department': "Don't Let Me Down", 'sub_title': '…91111011211', 'timelimit': '01:05:01', 'id': 175}"]
is there a way we can change it w
See update answer. Use regular expression to replace all the single quotes for double quotes
|

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.