我创建了一个repl来演示如何成功添加记录,但是似乎过于复杂。
const crossfilter = require('crossfilter2')
//Initial data
const data = [
{id:1, tags:['a','b','c']},
{id:2, tags:['a','e','f']},
{id:3, tags:['m','n','o']},
{id:4, tags:['p','n','d']}
]
//Mock updated object from server
const updatedObject = {
id:2,tags:['a','n','o']
}
//Init crossfilter dataset
const cf = crossfilter(data)
// Add dimensions
let ids = cf.dimension(row=>row.id)
let tags = cf.dimension(row => { return row.tags
}, true)
//First lets filter by dimension of arrays with value "a"
console.log('Filter by tag "A"')
tags.filter('a')
//From the chosen list we PUT the record to the server and get as response the mocked updatedObject with changed data
ids.filter(val=>{
return val==updatedObject.id
})
/**
Starting from here this is the only way I've been able to successfully add the record
**/
tags.filter(null) //without this the the add() wont work as expected
console.log('Data before remove',cf.allFiltered())
cf.remove()
console.log('Data after remove',cf.allFiltered())
// We can remove the ids filter
ids.filter(null)
console.log('data without the removed record',cf.allFiltered())
// We can now add the new record
console.log('Add record to dataset')
cf.add([updatedObject])
console.log('With the new record',cf.allFiltered())
tags.filter('a')
/** We now have to reset the grouping with updated data **/
let tagGroup = tags.group() //We need to reset the grouping for the reduceCount to work as expected
console.log('The correct reduced count of tags',tagGroup.reduceCount().top(Infinity))
console.log('ALL RESULTS',cf.allFiltered())
有更好了解的人可以告诉我这是不是用了dimensions with array
还是没有想到的用例还是我只是想念一些东西
感谢所有帮助