Change Array Values Ionic

Hello. I’m having trouble figuring out how to change the value of one nested item in an array in Ionic 2. I’m doing a get from an API, then I want to base 64 decode one of the values before I present it to the user. I’m not having any luck with my syntax. I can only get it to just return data and not change the item. How do I loop on the results and make the change?

Data being returned: (I want to decode the image item for all - it’s a URL being stored in a table as base64. I then point to the URL in the html to show the image located there)

[
   {
        "name": "Blueberry",
        "image": "aHR0cHM6Ly9saDMuZ299waG90by5qcGc="
    }
    {
        "name": "Apple",
        "image": "aHR0cHM6Ly9saDMuZ299waG90by5qcGc="
    }
    {
        "name": "Banana",
        "image": "aHR0cHM6Ly9saDMuZ299waG90by5qcGc="
    }
]

My code:

this.http.get('https://somestuff.com/api/fruit/' +body, {headers: headers}) 
    
      .map(res => {
        return res.json().map((item) => {
          item.image = window.atob(item.image);
          return item;
        })
      })
    	.subscribe(data => {
    		this.fruit = data;
            this.count = this.fruit.length;
        },
          err => {                     
            console.log(err);
                  }
             );

      console.log("new fruit images values? " +this.fruit);
    }

The placement of the console.log() statement is outside the scope of the asynchronous work, so it will not reflect the transformation. Move it inside the subscribe block and you should see what you expect.

1 Like