更改对象中的quantity_available,并读取到文件中,using node

r8xiu3jd  于 2022-12-12  发布在  Node.js
关注(0)|答案(2)|浏览(98)

我想更改quantity_available,并以相同的格式保存回文件。这是一个包含对象数组的对象。我使用节点。我首先从文件阅读变量products,对象看起来像这样。
第一个
//我想要的只是改变quantity_available。我希望能够检索对象的修改部分并写回文件。我读取了对象并解析了它,现在我需要修改。我将toString对象并重写到文件中。

uqzxnwby

uqzxnwby1#

使用减少

如果我理解得很好,您想知道如何操作该对象中的数据。
reduce是一种很好的方法,它可以遍历一个数组,然后返回所需的值

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;

}, {});

我添加了一个更大的完整示例的片段,如何获得对象与操作和更改您可能想要的。

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);
mklgxw1f

mklgxw1f2#

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);

相关问题