0
var data =  '[{"type":"product","id":1,"label":"Size","placeholder":"Select Size","description":"","defaultValue"
:{"text":"Size30","price":"20"},"choices":[{"text":"Size30","price":"20","isSelected":"true"},{"text"
:"Size32","price":"22","isSelected":false},{"text":"Size34","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":2,"label":"Color","placeholder":"Select Color","description":"","defaultValue"
:{"text":"Black","price":"10"},"choices":[{"text":"Black","price":"10","isSelected":"true"},{"text"
:"Green","price":"22","isSelected":false},{"text":"Red","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":3,"label":"Rise","placeholder":"Select Rise","description":"","defaultValue"
:{"text":"Low","price":"8"},"choices":[{"text":"High","price":"12","isSelected":"true"},{"text"
:"Low","price":"8","isSelected":false}],"conditionalLogic"
:""}]';

Here I have posted my JSON data. I want to get all the defaultValue in JSON/Array format. My output should be like-

 defaultValues:['Size30','Black','Low']

How to manage that in the foreach loop?

my code :

var otherSelectedOption;
angular.forEach(data, function(optionValue, optionKey) {
      if (optionValue.defaultValue.text) {
          otherSelectedOption = (optionValue.defaultValue.text);
      }
      selectedOption = {defaultValues: otherSelectedOption};
      console.log(selectedOption);          
});
2
  • 1
    data ===> JSON.parse(data) ...... i.e, angular.forEach(JSON.parse(data), Commented Oct 8, 2016 at 9:23
  • var r = JSON.parse(data).map(x=>x.defaultValues) Commented Oct 8, 2016 at 9:24

1 Answer 1

2

Your JSON is not valid, since objects are not separated by comma ,

Suppose this is the JSON

var obj = '[{"type":"product","id":1,"label":"Size","placeholder":"Select Size","description":"","defaultValue"
:{"text":"Size30","price":"20"},"choices":[{"text":"Size30","price":"20","isSelected":"true"},{"text"
:"Size32","price":"22","isSelected":false},{"text":"Size34","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":2,"label":"Color","placeholder":"Select Color","description":"","defaultValue"
:{"text":"Black","price":"10"},"choices":[{"text":"Black","price":"10","isSelected":"true"},{"text"
:"Green","price":"22","isSelected":false},{"text":"Red","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":3,"label":"Rise","placeholder":"Select Rise","description":"","defaultValue"
:{"text":"Low","price":"8"},"choices":[{"text":"High","price":"12","isSelected":"true"},{"text"
:"Low","price":"8","isSelected":false}],"conditionalLogic"
:""}]';

try

var arr = JSON.parse(obj).map( function(item){
   return item.defaultValue;
});
Sign up to request clarification or add additional context in comments.

2 Comments

@Moran sorry I am not able to open this link, can you share a fiddle instead? Also, what is it about?
no worries , it's the some code you wrote in a runnable manner :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.