【问题标题】:how to subtract Object value如何减去对象值
【发布时间】:2019-04-10 23:38:27
【问题描述】:

你能告诉我如何减去对象值吗? 假设我有一个物品的对象并且有库存

 let theObj = {
    pants: [
       {
       color: "Pink",
       stock: 80,
       price: 30.99
       } 
           ]
    };

这只是没有很多值和键,那个对象有很多数据呢?

插图: 如果用户想买裤子,在他/她选择了他/她想要的东西后,该对象将减去用户想要购买的数量,如果其他用户购买了该裤子和该颜色,则将减去该对象直到用完为止, 我希望这是有道理的

我希望我的问题和插图对你有意义

let theObj = {
    pants: [
       {
       color: "Pink",
       stock: 80,
       price: 30.99
       } 
           ]
    };
    
const theData = theObj["pants"].map(e => e.stock - 1)
console.log(theData)
console.log(theObj) // nothing change when i subtact it 

【问题讨论】:

  • 地图不改变对象
  • theObj["pants"].map(e => --e.stock)
  • 那怎么办?表明我们应该调用 theObj.pants["stock"] - 1 ?@LucaKiebel

标签: javascript arrays object if-statement iterator


【解决方案1】:

您不能使用 MAP,因为它不会改变任何东西。使用 forEach 或类似的循环

let theObj = {
  pants: [{
    color: "Pink",
    stock: 80,
    price: 30.99
  }]
};

theObj["pants"].forEach(e => e.stock -= 1)
console.log(theObj) 

你也许是这个意思?

let theObj = {
  pants: [{
    color: "Pink",
    stock: 80,
    price: 30.99
  }]
};

const purchase = { pants: { color: "Pink", quantity:2 }} // your user changes this

// this can be in a function

const item = Object.keys(purchase)[0];
theObj[item].forEach((e) => { if (e.color==purchase[item].color) e.stock -= purchase[item].quantity })
console.log(theObj)

【讨论】:

  • 为什么数据的结果是未定义的??对于我的示例来说,这只是一个简短的对象变量,如果我们的项目很多呢?我们应该使用这个功能吗?并在函数中使用那个 forEach?
  • 什么结果?我不再返回任何东西。请重新加载
  • 如果我们有很多物品,当用户想要挑选它时,库存会被减去怎么办?我们可以使用 if 语句,然后使用 forEach 来减去吗? @mlunjan
  • 这有意义吗?? @mplungjan
  • 你的意思是const getQuantityNonOrganic = fruits["non_organic"].map(e => e.quantity) - 如果你想在一个数组中计算多个项目,你需要减少
【解决方案2】:

您应该使用.forEach 而不是.forEach

let theObj = {
        pants: [
           {
           color: "Pink",
           stock: 80,
           price: 30.99
           } 
               ]
        };
        
theObj["pants"].forEach(e => e.stock -= 1)
    
console.log(theObj)

【讨论】:

  • 领先你 15 秒 ;)
  • 现在这和我的一样;)
  • @rlemon 是的,你是对的,我已经更新了我的答案
  • @mplungjan 哈哈你想要什么男人:)?
  • 只有一个! ;)
【解决方案3】:

您可以使用递归函数并将其泛化。遍历所有键并检查值是否为对象,如果对象则调用相同的函数(递归),如果值是数组,则迭代该数组并递归调用传递不同对象的相同函数

let theObj = {
  pants: [{
    color: "Pink",
    stock: 80,
    price: 30.99
  }]
};

function doOp(keyName, obj) {
  for (let keys in obj) {
    // check if current value is an array
    if (Array.isArray(obj[keys])) {
      obj[keys].forEach(function(item) {
        // call the same function and pass the objecr
        doOp(keyName, item);
      })
    } else if (typeof obj[keys] === 'object' && typeof obj[keys] !== null) {
      doOp(keyName, obj[keys])
    } else if (keys === keyName) {
      obj[keys] = obj[keys] - 1
    }
  }
}

doOp('stock', theObj)
console.log(theObj)
我期待结果是
let theObj = {
  pants: [{
    color: "Pink",
    stock: 79,
    price: 30.99
  }]
};

【讨论】:

  • 这是更快更好的方法吗?
猜你喜欢
  • 2020-10-29
  • 2018-08-24
  • 2020-07-26
  • 2020-08-23
  • 2021-10-24
  • 2017-10-03
  • 2011-01-12
  • 2018-08-02
  • 1970-01-01
相关资源
最近更新 更多