Link to home
Start Free TrialLog in
Avatar of Zolf
ZolfFlag for United Arab Emirates

asked on

Print JSON object and filter some values

Hi there,


How can I print the JSON data which I get from the server into the toastr. Also I want to filter the quantity_available out.

{"Raw_Material_3":false,"quantity_available":false,"Raw_Material_4":false}

Open in new window


else {
    console.log(result);
  this.toastr.warning(result, " Not enough Quantity", {
      enableHtml: true,
      disableTimeOut: true,
      closeButton: true,
      positionClass: 'toast-top-right'
  });
}

Open in new window


Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

You mean this:

https://jsfiddle.net/

const obj = {"Raw_Material_3":false,"quantity_available":false,"Raw_Material_4":false}
const {quantity_available, ...filtered} = obj;
const warning = `Not enough Quantity: ${Object.entries(filtered).map(([key,val]) => `${key}: ${val}`).join(", ")}`;
this.toastr.warning(result, warning, {/* the other parameters */});

Open in new window


Alternative for the Object.entries is to just use

const warning = `Not enough Quantity: ${JSON.stringify(filtered).slice(1,-1).replaceAll(/"/g," ")}`; 

Open in new window


or similar



Avatar of Zolf

ASKER

Thanks for your comment.

I am using Angular and this code is not working, it complains at replaceAll

ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial