How do I get data from JSON and pass it to an ionic 3 alert?

guys I’m trying to pass information from my JSON to this alert, I’m a little new with ionic, could someone help me?

TS:

  ionViewDidLoad() {
    this.qrCodeProvide.getBrowsers().then((data: any)=>{
      data.map((name) => {
        console.log(name)
      })
      console.log(data, 'ok');
      this.browsers = data;
    });
    console.log('ionViewDidLoad VetwebPage');
  }

  //botao para enviar para camera
  openQrcode(){
    this.navCtrl.push(QrcodePage, {animate: true} );
  }

  //alert para mostrar informações do navegador
  presentAlert() {

    let alert = this.alertCtrl.create({
      title: 'Information',
      subTitle: 'Browser: ',
      message: 'Last active session on: {b.name}',
      buttons: ['Disconnect', 'Exit'],
    });
    alert.present();
  }

JSON:

{
  "browsers": [ {
    "id": "1",
    "name": "Chrome",
    "date": "20 de Junho 2021 - 14:00"
  },
  {
    "id": "2",
    "name": "Firefox",
    "date": "15 de Janeiro 2021 - 21:00"
  },
  {
    "id": "3",
    "name": "Safari",
    "date": "05 de Dezembro 2020 - 15:32"
  },
  {
    "id": "4",
    "name": "Opera",
    "date": "07 de Março 2021 - 14:21"
  },
  {
    "id": "5",
    "name": "Edge",
    "date": "25 de Junho 2021"
  }]
}

change this to message: 'Last active session on: ' + this.browsers.name

1 Like

thank you so much for the reply Richard :slight_smile: everything worked out

Do everything you can to avoid any in your code. You definitely don’t need it here. Ditto on content-free variable names like data. Call it browsers or something that more accurately describes what it is, or your code becomes really hard to read when you come back to it after a while.

Assuming data is an array here (which would be easier without the aforementioned any), Array.map is for making one array out of another. forEach would make more sense here.