Home > Enterprise >  Change quantity_available in the object, and read to a file. using node
Change quantity_available in the object, and read to a file. using node

Time:12-09

I would like to change the quantity_available, and save back to a file with the same format. This is an object with an array of objects. I am using node. I am first reading from a file to a variable products the object looks like this.

{
  fish: [
    {
      name: 'Red Crown Tail',
      price: 20,
      image: 'redcrowntail.png',
      quantity_available: 139
    },
    {
      name: 'Blue Half Moon',
      price: 30,
      image: 'halfmoonbetta.png',
      quantity_available: 100
    },
    {
      name: 'Black Crown Tail',
      price: 15,
      image: 'blackcrowntail.png',
      quantity_available: 137
    },
    {
      name: 'Galaxy Double Tail',
      price: 25,
      image: 'gal.png',
      quantity_available: 149
    },
    {
      name: 'Opal Crown Tail',
      price: 35,
      image: 'opal.png',
      quantity_available: 147
    },
    {
      name: 'Blue Spade Tail',
      price: 23,
      image: 'spadetail.png',
      quantity_available: 146
    }
  ],
  tank: [
    {
      name: '3.5g Gallon Mini',
      price: 20,
      image: '3.5gallonminibowl.png',
      quantity_available: 129
    },
    {
      name: 'Tank2',
      price: 30,
      image: 'halfmoonbetta.png',
      quantity_available: 139
    },
    {
      name: 'Tank3',
      price: 15,
      image: 'blackcrowntail.png',
      quantity_available: 150
    },
    {
      name: 'Tank4',
      price: 25,
      image: 'gal.png',
      quantity_available: 150
    },
    {
      name: 'Tank5',
      price: 35,
      image: 'opal.png',
      quantity_available: 146
    },
    {
      name: 'Tank6',
      price: 23,
      image: 'spadetail.png',
      quantity_available: 150
    }
  ],
  plant: [
    {
      name: 'Plant',
      price: 10,
      image: 'AnubiasPlant.jpeg',
      quantity_available: 40
    },
    {
      name: 'Cry Plant',
      price: 12,
      image: 'cry.jpeg',
      quantity_available: 150
    },
    {
      name: 'Sw Plant',
      price: 13,
      image: 'SwordPlant.jpeg',
      quantity_available: 144
    },
    {
      name: 'Pink Plant',
      price: 20,
      image: 'PinkPlant.png',
      quantity_available: 150
    },
    {
      name: 'Rotala Plant',
      price: 23,
      image: 'rotalaPlant.jpeg',
      quantity_available: 140
    },
    {
      name: 'Vallisneria Plant',
      price: 26,
      image: 'vallisneriaPlant.jpeg',
      quantity_available: 150
    }
  ]
}
var data2 = fs.readFileSync(pname, 'utf-8');
var products = JSON.parse(data2);

// i need a double loop but i am lost on how to start. '
 for(product in products){
  for (i=0; i<product.length; i  ){
  console.log(products[product][i].quantity_available);
  }
 }

// All I want is to change the quantity_available. I want to be able to retrieve that part of the object modify and write back to the file. I read the object parsed it and now I need to modify. I will toString the object and rewrite to the file.

CodePudding user response:

use reduce

If I understand well, you want to know how to manipulate the data in this object. I suppose you already know how to read and write to the file

reduce is good to go over an array and get back what you want

Object.entries(products).reduce((acc, product) => {
  const [productKey, productArray] = product;

  const updatedProductArray = productArray.map(item => {
    // for example change all quantity_available to be 10
    item.quantity_available = 10;
    return { ...item, };
  });

  acc[productKey] = updatedProductArray;
  return acc;

}, {});

I added a snippet with larger full example, how to get object with manipulation and changes you may want.

const products = {
  fish: [
    {
      name: 'Red Crown Tail',
      price: 20,
      image: 'redcrowntail.png',
      quantity_available: 139
    },
    {
      name: 'Blue Half Moon',
      price: 30,
      image: 'halfmoonbetta.png',
      quantity_available: 100
    },
    {
      name: 'Black Crown Tail',
      price: 15,
      image: 'blackcrowntail.png',
      quantity_available: 137
    },
    {
      name: 'Galaxy Double Tail',
      price: 25,
      image: 'gal.png',
      quantity_available: 149
    },
    {
      name: 'Opal Crown Tail',
      price: 35,
      image: 'opal.png',
      quantity_available: 147
    },
    {
      name: 'Blue Spade Tail',
      price: 23,
      image: 'spadetail.png',
      quantity_available: 146
    }
  ],
  tank: [
    {
      name: '3.5g Gallon Mini',
      price: 20,
      image: '3.5gallonminibowl.png',
      quantity_available: 129
    },
    {
      name: 'Tank2',
      price: 30,
      image: 'halfmoonbetta.png',
      quantity_available: 139
    },
    {
      name: 'Tank3',
      price: 15,
      image: 'blackcrowntail.png',
      quantity_available: 150
    },
    {
      name: 'Tank4',
      price: 25,
      image: 'gal.png',
      quantity_available: 150
    },
    {
      name: 'Tank5',
      price: 35,
      image: 'opal.png',
      quantity_available: 146
    },
    {
      name: 'Tank6',
      price: 23,
      image: 'spadetail.png',
      quantity_available: 150
    }
  ],
  plant: [
    {
      name: 'Plant',
      price: 10,
      image: 'AnubiasPlant.jpeg',
      quantity_available: 40
    },
    {
      name: 'Cry Plant',
      price: 12,
      image: 'cry.jpeg',
      quantity_available: 150
    },
    {
      name: 'Sw Plant',
      price: 13,
      image: 'SwordPlant.jpeg',
      quantity_available: 144
    },
    {
      name: 'Pink Plant',
      price: 20,
      image: 'PinkPlant.png',
      quantity_available: 150
    },
    {
      name: 'Rotala Plant',
      price: 23,
      image: 'rotalaPlant.jpeg',
      quantity_available: 140
    },
    {
      name: 'Vallisneria Plant',
      price: 26,
      image: 'vallisneriaPlant.jpeg',
      quantity_available: 150
    }
  ]
}

const updatedProducts = Object.entries(products).reduce((acc, product) => {
  const [productKey, productArray] = product;

  const updatedProductArray = productArray.map(item => {
    if (productKey === 'fish') {
      if (item.name === 'Red Crown Tail') {
        item.quantity_available = item.quantity_available * 2;
        item.whatChanged = 'quantity_available multipled by 2';
      } else {
        item.quantity_available = item.quantity_available * 3;
        item.whatChanged = 'quantity_available multipled by 3';
      }
      return { ...item };
    }

    if (productKey === 'tank') {
      item.quantity_available = item.quantity_available   50;
      item.whatChanged = 'quantity_available added 50';
      return { ...item };
    }

    // every other case will do that
    item.quantity_available = item.quantity_available - 40;
    item.whatChanged = 'quantity_available subtracted 40';
    return { ...item };
  });


  acc[productKey] = updatedProductArray;
  return acc;

}, {});

console.log('updatedProducts', updatedProducts);

CodePudding user response:

let data2 = fs.readFileSync(pname, 'utf-8');
let products = JSON.parse(data2);

products = Object.keys(products).map((key) => ({
  [key]: products[key].map((product) => ({
    ...product,
    quantity_available: product.quantity_available   1,
  })),
}));
console.log(products);
  • Related