Home > database >  How to update a json object with specified index of Array and adding value(number) with already exis
How to update a json object with specified index of Array and adding value(number) with already exis

Time:07-08

I want to add a number to an existing value for a specific user.

In this example, First time I want to add saving: 500 to Kramer so his saving will be 600(100 500)

Second time I want to add saving: 1000 to George so his saving will be 4000(3000 1000)

[
{
"userID": "abc",
"displayName": "Gerry Seinfeld",
"saving": 10000000000
},
{
"userID": "cde",
"displayName": "George Costanza",
"saving": 3000
},
{
"userID": "efg",
"displayName": "Elaine Benes",
"saving": 20000000
},
{
"userID": "hij",
"displayName": "Cosmo Kramer",
"saving": 100
}
]

expected Kramer's saving 100 -> 600

[
{
"userID": "abc",
"displayName": "Gerry Seinfeld",
"saving": 10000000000
},
{
"userID": "cde",
"displayName": "George Costanza",
"saving": 3000
},
{
"userID": "efg",
"displayName": "Elaine Benes",
"saving": 20000000
},
{
"userID": "hij",
"displayName": "Cosmo Kramer",
"saving": 600
},
]

expected George's saving 3000 -> 4000

[
{
"userID": "abc",
"displayName": "Gerry Seinfeld",
"saving": 10000000000
},
{
"userID": "cde",
"displayName": "George Costanza",
"saving": 4000
},
{
"userID": "efg",
"displayName": "Elaine Benes",
"saving": 20000000
},
{
"userID": "hij",
"displayName": "Cosmo Kramer",
"saving": 600
},
]

I tried following but it update all the saving value

const [use, setUser] = useState([]);

 const chipMoney = async () => {
    const response = await fetch('https://brabrabra.io');
    let data = await response.json();
    let addMoney = getRandomMoeny();
    let userIndex = getRandomIndex();

    let updateData = data.map(obj => {
      return {...obj, score: addMoney}
    })
    const sortedData = updateData.sort((a,b) => b.score - a.score);
    setUser(sortedData);
  }

  const getRandomMoeny = () => {
    const MIN = 50;
    const MAX = 10000;
    let randomFloat = Math.random();
    let randomInt = Math.floor(randomFloat * (MAX - MIN))   MIN;
    return randomInt
  }


      const getRandomIndex = () => {
    const MIN = 0;
    const MAX = 4;
    let randomFloat = Math.random();
    let randomInt = Math.floor(randomFloat * (MAX - MIN))   MIN;
    return randomInt
  }

CodePudding user response:

This should work

 const chipMoney = async () => {
    const response = await fetch('https://brabrabra.io');
    let data = await response.json();
    let addMoney = getRandomMoeny();
    let userIndex = getRandomIndex();

    let updateData = data.map((obj, index) => {
      if (index === userIndex) {
        return {...obj, score: obj.score   addMoney}
      }
      return {...obj}
    })
    const sortedData = updateData.sort((a,b) => b.score - a.score);
    setUser(sortedData);
}

OR if you don't mind changing objects:

const chipMoney = async () => {
    const response = await fetch('https://brabrabra.io');
    let data = await response.json();
    let addMoney = getRandomMoeny();
    let userIndex = getRandomIndex();

    let updateData = [...data]
    updateDate[userIndex].score  = addMoney
    const sortedData = updateData.sort((a,b) => b.score - a.score);
    setUser(sortedData);
}
  • Related