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

179
Views
How can += be used when adding to an object property that may not exist?

Consider an array of objects

myArray = [
  {date: 'date1', amount: 1},
  {date: 'date1', amount: 2},
  {date: 'date2', amount: 3},
  {date: 'date2', amount: 4},
]

I want to end up with values summed in a object like so:

{
  date1: 3,
  date2: 7
}

This works:

let myObj = {}
myArray.forEach(arrayObj=>{
  myObj[arrayObj.date] ? myObj[arrayObj.date] += arrayObj.amount : myObj[arrayObj.date] = obj.amount
})

This would be cleaner but does not work

let myObj = {}
myArray.forEach(arrayObj=>{
  myObj[arrayObj.date] += arrayObj.amount
})

Question: is there a way to do this without checking for the existence of the property?

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

0

There's no way to get around repeating myObj[arrayObj.date] twice, but you can make things shorter by unconditionally assigning to it, and alternating || it with 0.

myArray = [
  {date: 'date1', amount: 1},
  {date: 'date1', amount: 2},
  {date: 'date2', amount: 3},
  {date: 'date2', amount: 4},
]
const result = {};
for (const { date, amount } of myArray) {
  result[date] = (result[date] || 0) + amount;
}
console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

You can utilize a Proxy to create a DefaultDict object, similar to that in python. DefaultDict returns a predefined value for missing keys, instead of undefined.

function DefaultDict(value) {
    return new Proxy({}, {
        get(target, key) {
            return key in target ? target[key] : value
        }
    })
}

// example:


myArray = [
  {date: 'date1', amount: 1},
  {date: 'date1', amount: 2},
  {date: 'date2', amount: 3},
  {date: 'date2', amount: 4},
]

myObj = DefaultDict(0)

myArray.forEach(v => myObj[v.date] += v.amount)

console.log(myObj)

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!