Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
How to increment values in an array json file based of from a name?

Example JSON file:

[
 {
   "discordId": "9273927302020",
   "characters": [
     {
     "name": "Rare_Character",
     "value": 1
     },
     {
     "name": "Ultra_Rare_Character",
     "value": 1
     }
   ]
 }
]

Let's just say for example I ran this simple gacha and got 4 characters:

let i = 1
var picks = []
while(i <= 4){
  const { pick } = gacha.simple(alpha)
  picks.push(pick)
  i++
}

Now, picks has an array like this:

[
  {
    "name": "Common_Character"
  },
    {
    "name": "Ultra_Rare_Character"
  },
    {
    "name": "Common_Character"
  },
    {
    "name": "Rare_Character"
  }
]

How do I increment the value in My Example JSON file based on the name from what I got in my gacha results picks while ignoring the Common_Character and only passing those Rare and Ultra_Rare ones?

I've tried filtering them like this:

var filter = picks.filter(t => t.name === 'Rare_Character' || t.name === 'Ultra_Rare_Character')

Now I don't know how to increase those values in my JSON file and what if in the gacha results I got two Rare_Characters or Ultra_Rare_Character

I'm using fs to read my JSON file but I just don't know the logic to increase values

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

const src = [
 {
   "discordId": "9273927302020",
   "characters": [
     {
     "name": "Rare_Character",
     "value": 1
     },
     {
     "name": "Ultra_Rare_Character",
     "value": 1
     }
   ]
 }
];

const gacha = [
  {
    "name": "Common_Character"
  },
    {
    "name": "Ultra_Rare_Character"
  },
    {
    "name": "Common_Character"
  },
    {
    "name": "Rare_Character"
  }
];

const updateValues = (src, gacha) => {
  const gachaSums = gacha.reduce((collector, current) => { 
    collector[current.name] = (collector[current.name] | 0) + 1;
    return collector;
  }, {});
  src.characters.forEach(srcChar => {
    gachaSums[srcChar.name] = srcChar.value + (gachaSums[srcChar.name] | 0);
  });
  src.characters = Object.entries(gachaSums).map(([key, value]) => 
    ({ name: key, value: value })
  );
  return src;
}

console.log(updateValues(src[0], gacha));

Maybe this version could help

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!