Home > Software engineering >  How to get a specific data from object of array in angular
How to get a specific data from object of array in angular

Time:04-25

I wanna know that how to get the data HERE IS MY node.js

app.post('/pathname', function (req, res){
   fs.readfile('filepath', 'utf8', function(err, data){
     if(data){
        let valueofdata = JSON.parse(data);
        let anothervalue = JSON.stringify(valueofdata[0]);
        res.send(anothervalue);
}

My JSON file is

[{
   "number":[{
           "data":"one",
           "data":"two",
           "data":"three"
            }],
"number1":[{
           "data":"four",
           "data":"five",
           "data":"six"
            }],

}]

My ANGULAR file

     numberval:any;
  ngOnInit(): void {
    this.numberval = this.service.numbervalueall;  --> the value (number) is stored in service 
    console.log(this.numberval)
  }

  numberall(data:any){
    this.http.post('http://localhost:4001/pathname', data, {responseType : "text"} )
    .subscribe(res => {
        console.log(res)
        this.numbersname = JSON.parse(res) --> Data from node.js is stored in here
        console.log(this.numbersname )
      })
  }


numbersname!:any;

 numberdata(){
  this.numberall(this.service.numbervalueall)
 }

 sampledata(){
  console.log(this.service.citydata)  
   setTimeout(() => {
    this.numberall(this.service.citydata)
    console.log(this.hotelvalue)
  },100);   
 }

How can I get the specific value from object data stored in res I used res.this.service.numbervalueall but can't get the value. Please Help me with this.

CodePudding user response:

In your ANGULAR file create new variable as datavalue or your choice

datavalue:any;
  numberall(data:any){
    this.http.post('http://localhost:4001/pathname', data, {responseType : "text"} )
    .subscribe(res => {
        console.log(res)
        this.numbersname = JSON.parse(res) 
        this.datavalue = this.numbersname[numbervalueall] --> here u get the specific data in datavalue
      })
  }
  • Related