3

I want to extract only 'phaseValue' part from below json and store that into array.

{  
   "templateName":"TemplateExample",
   "phaseExecutions":{  
      "PRE":[  
         {  
            "phaseType":"text",
            "phaseValue":"enter your name"
         },
         {  
            "phaseType":"number",
            "phaseValue":"enter your mobile number"
         },
         {  
            "phaseType":"email",
            "phaseValue":"enter your email"
         }
      ]
   }
}

Above data I am getting from below code

getTemplateByName():any
{


    this.httpService.get('http://localhost:57611/Api/Employee/GetTemplateByName/'+this.selectedTemplate).subscribe(  
          data => {  
           this.templateInJsonFormat = data;   
           this.getTemplateFromSubscribe(this.templateInJsonFormat);
          }
       );
    }

    getTemplateFromSubscribe(temp:any)
    {
         this.finalTemplateFromSubscribe = temp;  
         console.log(this.finalTemplateFromSubscribe);    
    }

I am using typescript 3.5.3. Please help.

1

4 Answers 4

6

const arr = object.phaseExecutions.PRE.map(item => item.phaseValue)

Sign up to request clarification or add additional context in comments.

2 Comments

Please have a look at updated code in my question.I am getting that json data from api.
Yes, this can be applied anywhere regardless of specific implementation and code organization.
5

Live demo: https://stackblitz.com/edit/typescript-m9nv6q

const data = 
{  
   "templateName":"TemplateExample",
   "phaseExecutions":{  
      "PRE":[  
         {  
            "phaseType":"text",
            "phaseValue":"enter your name"
         },
         {  
            "phaseType":"number",
            "phaseValue":"enter your mobile number"
         },
         {  
            "phaseType":"email",
            "phaseValue":"enter your email"
         }
      ]
   }
}

const arr = data.phaseExecutions.PRE.map(p=>p.phaseValue);

I recommend you to take a look to the .map() documentation here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

1 Comment

Please have a look at updated code in my question.I am getting that json data from api.
5
obj = {  
   "templateName":"TemplateExample",
   "phaseExecutions":{  
      "PRE":[  
         {  
            "phaseType":"text",
            "phaseValue":"enter your name"
         },
         {  
            "phaseType":"number",
            "phaseValue":"enter your mobile number"
         },enter code here
         {  
            "phaseType":"email",
            "phaseValue":"enter your email"
         }
      ]
   }
}

var phaseValueArr = obj.phaseExecutions.PRE.map(x => x.phaseValue);

phaseValueArr is the required array

1 Comment

Please have a look at updated code in my question.I am getting that json data from api.
4

Try like this:

Working Demo

result = []

 this.httpService
  .get(
    "http://localhost:57611/Api/Employee/GetTemplateByName/" +
      this.selectedTemplate
  )
  .subscribe(data => {
    this.templateInJsonFormat = data;
    this.result = this.templateInJsonFormat.phaseExecutions.PRE.map(x => x.phaseValue)
    this.getTemplateFromSubscribe(this.templateInJsonFormat);
  });

4 Comments

Please have a look at updated code in my question.I am getting that json data from api.
Check the modified answer
It is showing error: [ts] Property 'phaseExecutions' does not exist on type 'Object'.
Check the modified answer

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.