0

I have the following json data, which was read into an arrayList and saved in local-storage:

[
    {
        "id": 1,
        "name": "Albany",
        "manufacture": "Albany Superior Low Gi Sliced Brown Seed Bread 700g",
        "price": 15.49,
        "category": "Food",
        "type": "Breads",
        "image": "data:image/jpeg;base64,/9j/4AAQSkZJRgA..."
    },
    {
        "id": 2,
        "name": "Blue Ribbon",
        "manufacture": "Blue Ribbon Brown Plus Low Gi Bread 700g",
        "price": 13.99,
        "category": "Food",
        "type": "Breads",
        "image": "data:image/octet-stream;base64,/9j/4AAQSkZJRgABA..."
    },
    {...},
    {...},
    ...
]

I then wrote a class that will retrieve these data from local-storage, which I filtered for a specific product type. The class is as follows:

export class MilkCreamComponent implements OnInit {

  allProducts: Array<Product> = [];
  quantity: number = 1;
  resultArray:any;
  milkProducts =[]
  newMilkProducts = [];

  constructor( private prod: ProductService, public _DomSanitizer: DomSanitizer) { }

  ngOnInit() {

    this.allProducts = JSON.parse(localStorage.getItem('product-data') );
    //console.log( JSON.stringify( this.allProducts ) );

    var productMilk = this.allProducts.filter(item => item.type === 'Milk');
    this.milkProducts = productMilk;
    //console.log( this.milkProducts );

      for (var i=0; i < this.milkProducts.length / 4; i++) {
        var imageString = this.milkProducts[i].image;
        var edittedImageString = imageString.substring(imageString.indexOf(",") + 1 );
        var newImageStringFormat = "data:image/jpeg;base64," + edittedImageString;

        if ( edittedImageString ===  this.milkProducts[i].image.substring(this.milkProducts[i].image.indexOf(",") + 1) ){

          var index = this.milkProducts.indexOf( this.milkProducts[i] );

          if (index !== -1) {
            this.milkProducts.indexOf[index] =  this.milkProducts[i].id, this.milkProducts[i].name, this.milkProducts[i].manufacture,
                                this.milkProducts[i].price, this.milkProducts[i].category, this.milkProducts[i].type, newImageStringFormat;
            console.log (  this.milkProducts );
            }
        }
        else{
          console.log("Images Are Not Equal\nSee milk-cream.component.ts\nSee Image Conversion Codes");
        }
      }
    //console.log( this.newMilkProducts );

  }
}

interface Product {
  id: number;
  name: string;
  manufacture: string;
  price: number;
  category: string;
  type: string;
  image: string;
}

What I want to do is to replace specific values of a key's, and then console-log that arrayList.

1 Answer 1

1

Try this:

this.allProducts = JSON.parse(localStorage.getItem('product-data'));

this.allProducts.forEach(function(item) {
  if (item.type === "Milk") {
    var edittedImageString = item.image.substring(item.image.indexOf(",") + 1 );
    item.image = "data:image/jpeg;base64," + edittedImageString;
  }
});
Sign up to request clarification or add additional context in comments.

Comments

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.