Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create an object from an array with multiple ids, using the id as a key and knowing how many identical ids each have

Create an object from an array with multiple ids, using the id as a key and knowing how many identical ids each have

function createOrder(organization: TreeOrganization, valueId: number): number {
    return organization.values
        .map(o => o.parentId)
        .reduce((a, c) => {
            return a[`${c}`] === undefined
                ? { ...a, [`${c}`]: 1 }
                : { ...a, ...{ [`${c}`]: a[`${c}`] + 1 } }
        }, {} as { [ind: string]: number })
}

author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment