Noob Question

Please tell me a better way to do this if u know of any? Is ternary only option?
5 Replies
Perfect
PerfectOP3y ago
and by "this" i mean the has own property check (only adding when it exists, setting when it doesnt)
Neto
Neto3y ago
you can
Neto
Neto3y ago
Perfect
PerfectOP3y ago
@Neto ah nice, thank you! much cleaner the logic is not the same but I see what I can do appreciate it
Neto
Neto3y ago
the full snippet
const accumulator: Record<string, any> = {}

const postings = [
{
sceneId: '123',
count: 1,
dl: ''
},
{
sceneId: '123',
count: 2,
dl: ''
},
{
sceneId: '144',
count: 2,
dl: ''
}
]

for (const posting of postings) {
const { sceneId, count } = posting
if (accumulator[sceneId]) {
accumulator[sceneId].count += count
} else {
accumulator[sceneId] = { ...posting }
}
}
const accumulator: Record<string, any> = {}

const postings = [
{
sceneId: '123',
count: 1,
dl: ''
},
{
sceneId: '123',
count: 2,
dl: ''
},
{
sceneId: '144',
count: 2,
dl: ''
}
]

for (const posting of postings) {
const { sceneId, count } = posting
if (accumulator[sceneId]) {
accumulator[sceneId].count += count
} else {
accumulator[sceneId] = { ...posting }
}
}
Want results from more Discord servers?
Add your server