2

users.json

[
  {
    "name": "Rubs",
    "uuid": "9e24f3f5-7089-4f3c-bf63-211c24a0744e",
    "balance": 500
  },
  {
    "name": "Foxitic",
    "uuid": "39f6521c-5a18-48e4-8c84-6f1bb6c8de90",
    "balance": 500
  },
  {
    "name": "Jxsh",
    "uuid": "5f06ea31-3a20-43fc-8ce8-60ac7e7b0971",
    "balance": 500
  },
  {
    "name": "Bopeebo",
    "uuid": "91a5d15b-cca0-44e9-a513-91fe7ab05715",
    "balance": 500
  }
]

If I wanted to update the "balance" of an individual with the specified "name", how would I go about doing that?

I am already looping through each to find which object is the specified user's, I just need to be able to edit that object, and more specifically the balance. Thanks for all help you can give, you guys are the real heroes <3

2
  • 2
    You can do something like foundObj.balance = 100, which will update the found object (that you've obtained using your loop) to 100 Commented Jun 5, 2021 at 0:51
  • How do you read the file? Are you familiar with JSON.parse() and JSON.stringify() ? Commented Jun 5, 2021 at 0:56

5 Answers 5

2

I fully agree with Dinesh Patil's solution. however, writing in arrow function makes the map() method a lot more easier to understand.

   const jsonObj = [
    {name: 'abc', balance: 400},
    {name: 'jkl', balance: 400},
    {name: 'erp', balance: 400}
   ]

   pos = jsonObj.map(val => val.name).indexOf('jkl')

   jsonObj[pos].balance = 200
Sign up to request clarification or add additional context in comments.

Comments

0

Don't use forEach... Use Arra.find https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find which skip the rest when something found...

yourArrry.find((el)=>el.name == nameToFind).balance = newBalance;

You can store return value of find in a variable and assign new value after verifying when it is not undefined

Comments

0

Suggest the idea like this.

  • you have to know the 'name' (ie. 'Jxsh') you want to update the balance.
  • then find out an index of that name in the users array (ie. index = 2).
  • use that index (ie. users[2]) to update the balance to amount you want (ie. users[2].balabce=900 ).

Comments

0

In my case I would do something like this:

let ids = {};    
let arr = [
    {
        "name": "Rubs",
        "uuid": "9e24f3f5-7089-4f3c-bf63-211c24a0744e",
        "balance": 500
    },
    {
        "name": "Foxitic",
        "uuid": "39f6521c-5a18-48e4-8c84-6f1bb6c8de90",
        "balance": 500
    },
    {
        "name": "Jxsh",
        "uuid": "5f06ea31-3a20-43fc-8ce8-60ac7e7b0971",
        "balance": 500
    },
    {
        "name": "Bopeebo",
        "uuid": "91a5d15b-cca0-44e9-a513-91fe7ab05715",
        "balance": 500
    }
  ]
    arr.map((item, index) => {ids[item.uuid] = index});

    ////update balance code
    arr[ids["91a5d15b-cca0-44e9-a513-91fe7ab05715"]].balance = 333;

Comments

0

This link makes be useful for you. https://stackoverflow.com/a/16008853/10802524

Following ways can help you to achieve what you want. First

  • You can iterate a loop for each object.
  • Add if condition to match the specific name.
  • Finally, modify the balance of that index of the matched name.
const jsonObj = [{ name : "xyz", balance: 500},{ name : "abc", balance: 800},{ name : "efg", balance: 50}];

for(let obj of jsonObj){
If(obj.name === "abc")
     obj.balance = 700; // changing value for balance
}

Second

  • You can use the map to get the index of a specific name.
  • Modify the balance's value of that index.
const jsonObj = [{ name : "xyz", balance: 500},{ name : "abc", balance: 800},{ name : "efg", balance: 50}];

pos = jsonObj.map((obj)=> return obj.name;).indexOf('abc')

jsonObj[pos].balance = 700;

Hope this will help you

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.