Skip to content

Instantly share code, notes, and snippets.

@omginbd
Last active March 12, 2019 02:10
Show Gist options
  • Save omginbd/04207eef6c30dbd8a9fa20afd53f72ea to your computer and use it in GitHub Desktop.
Save omginbd/04207eef6c30dbd8a9fa20afd53f72ea to your computer and use it in GitHub Desktop.
const assert = require('assert')
const test = [
{
title: 'hi',
values: [
{
count: '1',
value: 'fun'
},
{
count: '0',
value: 'no fun'
}
]
},
{
title: 'hey',
values: []
}
]
const final = [
{
title: 'hi',
values: [
{
count: '1',
value: 'fun'
}
]
}
]
const transform = categories => {
return categories.reduce((memo, category) => {
category.values = category.values.filter(value => +value.count)
return category.values.length ? memo.concat([category]) : memo
}, [])
}
const main = () => {
const transformed = transform(test)
assert(transformed[0].values.length === 1)
assert(transformed.length === 1)
console.log('it worked')
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment