Noob Question

Please tell me a better way to do this if u know of any? Is ternary only option?
5 Replies
Perfect
Perfect2y ago
and by "this" i mean the has own property check (only adding when it exists, setting when it doesnt)
Neto
Neto2y ago
you can
Neto
Neto2y ago
Perfect
Perfect2y ago
@Neto ah nice, thank you! much cleaner the logic is not the same but I see what I can do appreciate it
Neto
Neto2y 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