Zr Classic
Zr Classic

Reputation: 313

how to subtract Object value

can you tell me how to subtract the object value, let say that I have an Object for the item and has stock

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

that is just not many values and keys, how about that Object has a lot of data?

for illustrations : if the user wants to buy the pant and after he/she pick what he/she wanted, that object will be subtracted by what user want to buy how many they want, and it will be subtracted if other users buy that pants and that colo until run out, I hope this makes sense

i hope my question and illustration make sense for you

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 

Upvotes: 0

Views: 2736

Answers (3)

brk
brk

Reputation: 50326

You can use a recursive function and generalize it. Iterate through all the keys and check if the value is an object, if object then call the same function(recursion) , if the value is an array then iterate that array and recursively call the same function passing different object

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)
I am expecting the result to be

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

Upvotes: 0

mplungjan
mplungjan

Reputation: 178285

You cannot use MAP since it does not change anything. Use forEach or similar loop

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

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

You perhaps mean this?

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)

Upvotes: 1

Artyom Amiryan
Artyom Amiryan

Reputation: 2966

instead .map you should do .forEach

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

Upvotes: 1

Related Questions