[英]Filter array of Object values in React chartjs2 dataset
I am implementing a dashboard which has a search input field in the parent component, the value gets passed to a chart component as props. 我正在实现一个仪表板,该仪表板在父组件中具有搜索输入字段,该值作为道具传递给图表组件。 I've been trying really hard to filter the data in the chart onChange of search string.
我一直在努力筛选onChange搜索字符串图表中的数据。 Below is my chart setup.
以下是我的图表设置。
listitems = [{"App_Id":"10426","YearMonth":"201808","App_Name":"XXX- YYY","department":"XXXX","s_name":"Marl"},{"App_Id":"144689","YearMonth":"201808","App_Name":"ttttt","department":"uuuuuu","s_name":"Steveu",}]
State
州
state = {
data: this.props.listitems,
filtervalue: ''
}
Chart Data
图表数据
const data = {
labels: this.state.data.map(x => x.App_Id),
datasets: [{
label: "Bugs",
data: this.state.data.map(x => x.code_bugs),
data1: this.state.data.map(x => x.App_Name),
data2: this.state.data.map(x => x.s_name),
backgroundColor: 'rgba(255,99,132,0.2)',
borderColor: 'rgba(255,99,132,1)',
borderWidth: 1,
hoverBackgroundColor: 'rgba(255,99,132,0.4)',
hoverBorderColor: 'rgba(255,99,132,1)'
}]
}; };
I am trying to use the a filter to search the values of the object and return a array of filter object which would then become the data for the chart. 我正在尝试使用一个过滤器来搜索对象的值并返回一个过滤器对象数组,该数组随后将成为图表的数据。 This would mean use can interactively see the chart getting modified as filter changes in search box.
这意味着用户可以交互地查看图表,因为搜索框中的过滤器发生了更改。
Below is what i tried with lodash it doesn't work. 以下是我用lodash尝试的方法,它不起作用。 Please suggest
请建议
_.filter(listname,function(item) {return item.App_Name.lowerCase.indexOf('searchstring')>-1;}
Here is one way to achieve this. 这是实现此目的的一种方法。 Overall there are more than few others since it is a simple array filtering.
总体而言,由于它是一个简单的数组过滤,因此还有很多其他的东西。
I also made the filter with configurable key
so you can filter on any field. 我还使用可配置
key
制作了过滤器,因此您可以在任何字段上进行过滤。
Note : No need to use lodash
in this case since ES6 has more than enough to cover this. 注意 :在这种情况下,无需使用
lodash
,因为ES6足以满足要求。
listItems = [{ "App_Id": "10426", "YearMonth": "201808", "App_Name": "XXX- YYY", "department": "XXXX", "s_name": "Marl" }, { "App_Id": "144689", "YearMonth": "201808", "App_Name": "ttttt", "department": "uuuuuu", "s_name": "Steveu", }] const filterItems = (appName, key) => listItems.filter(i => i[key].indexOf(appName) >=0) console.log(filterItems('ttt', 'App_Name')) console.log(filterItems('XX', 'App_Name')) console.log(filterItems('Stev', 's_name'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.