Categories do not change despite I changed config.
https://codesandbox.io/s/highcharts-react-demo-j7hoi
I want to show categories like in config.
In API we can read:
type: Highcharts.AxisTypeValue
(...) In a category axis, the point names of the chart's series are used for
categories, if not a categories array is defined.
So, you also need to update the x values:
} else {
this.setState({
options: {
...,
series: [
{
data: [[0, 3], [1, 45], [2, 12]]
}
]
}
});
}
Live demo: https://codesandbox.io/s/highcharts-react-demo-4x8np
API: https://api.highcharts.com/highcharts/xAxis.type
Related
I am tring to use react-charts and the object they give as example data looks like this.
chartData: [
{
label: 'Series 1',
data: [
[0, 1],
[1, 2],
[2, 4],
[3, 2],
[4, 7],
],
},
],
I want to build my own "data" and replace this temporary data but the whole object of arrays in objects in arrays (Or whatever it is confuses me.
Can someone explain the nesting here.
Is it an array of 2 objects label and data and data` is an array of key value pairs. Is that accurate?
I'm kind of trying something like this...
let myData = []
res.data.payload.forEach(function (item, index) {
console.log(item, index)
myData[(index, item.odds)]
})
this.setState({ chartData[data]: myData })
Am I even close?
Thanks
You can do like this
let myData = []
res.data.payload.forEach(function (item, index) {
console.log(item, index)
myData.push([index, item.odds])
})
this.setState({ chartData: [{...this.state.chartData[0], data: myData}] })
I'm having an object like this
{
"GroupA": {
"Parent1": [1, 2, 3],
"Parent2": [1, 2, 3],
"Parent12": [1, 2, 3]
},
"GroupB": {
"Parent13": [1, 2, 3],
"Parent5": [1, 2, 3]
},
"GroupC": {
"Parent7": [1, 2, 3]
}
}
Now i want to filter this object by searching the name of the Parent
For example when I search parent1 the result should be
{
"GroupA": {
"Parent1": [1, 2, 3],
"Parent12": [1, 2, 3]
},
"GroupB": {
"Parent13": [1, 2, 3],
}
}
Here is my solution but it's not working correctly if a Group has many similar Parent name it only return the first one. And when I try to set state it filter like all wrong value
let newData = []
let catalogArr = Object.entries(catalogList)
const handleSearchElement = (e) => {
const value = e.target.value
catalogArr.forEach(catalog => {
let filteredKeys = Object.keys(catalog[1]).filter(name => name.toLowerCase().includes(value.toLowerCase()))
let valuesOfKey
if(filteredKeys[0]) {
valuesOfKey = {
[filteredKeys[0]]: Object.values(catalog[1][filteredKeys[0]])
}
newData.push([catalog[0], {...valuesOfKey}])
}
})
console.log(Object.fromEntries(newData));
setCatalogList(Object.fromEntries(newData))
// console.log(catalogList);
}
You can use Array#reduce to accomplish this pretty easily, however, all of the packing and unpacking of objects using Object.entries and Object.fromEntries to essentially treat them as arrays suggests you may be using the wrong data structure.
If you need to do this repeatedly, look into doing a one-off transformation that arranges the data for O(1) access, for example, by grouping on inner keys rather than outer keys (hard to say since I don't know the data or use case). Or, if you're mostly iterating, consider using arrays.
const data = {
"GroupA": {
"Parent1": [1, 2, 3],
"Parent2": [1, 2, 3],
"Parent12": [1, 2, 3]
},
"GroupB": {
"Parent13": [1, 2, 3],
"Parent5": [1, 2, 3]
},
"GroupC": {
"Parent7": [1, 2, 3]
}
};
const targetKey = "parent1";
const res = Object.entries(data).reduce((a, [k, v]) => {
const filtered = Object.entries(v).filter(([k, ]) =>
k.toLowerCase().includes(targetKey.toLowerCase())
);
if (filtered.length) {
a[k] = Object.fromEntries(filtered);
}
return a;
}, {});
console.log(res);
I have an array of arrays in MongoDB 4.2.2:
db.foo.insertOne({'foo': [[1, 3], [2], [3]]})
I'd like to remove elements of foo, first elements of which are greater than 1. But I cannot figure out how.
I tried something like this (and many more) but it does not pull anything:
db.foo.update({}, {$pull: {foo: {'0': {$gt: 1}}}})
Is it possible?
EDIT: Expected result:
db.foo.find({})
{ "_id": ObjectId("..."), "foo": [ [1, 3] ] }
If you are using MongoDB 4.2, you can make use of a pipeline in the new update. This allows you to pass an aggregation pipeline as the update argument:
db.foo.update({},[
{$addFields:{
foo:{
$filter:{
input:"$foo",
cond:{
$lte: [{$arrayElemAt: ["$$this", 0]}, 1]
}
}
}
}}
])
Say my event looks like:
purchase = {
items: ["pickle", "turtle", "lexicon"]
}
How do I count how many events have "pickle"?
Use the equal operator.
var count = new Keen.Query("count", {
event_collection: "purchases",
timeframe: "this_14_days",
filters: [
{
property_name: "items",
operator: "eq",
property_value: "pickle"
}
]
});
From the API reference for filters & their operators:
“Equal to” – Note that if your property’s value is an array, “eq” can be used to filter for values inside that array. For example, eq: 5 will match a value of [5, 6, 7].
I stuck with this bit and I can't progress - I guess solution is simple but I can't figure out. I'm trying to add entry in reducer so data in in would look something this:
state = {
entryId: {
entryName: ["something", "something2", "something3" /* and so on... */]
}
};
So far this is the closest I get, but, instead of adding new unique entry, it is replacing the one that is stored already. Also I need to be able to add this item to empty state where entryId, entryName doesn't exist yet to avoid error:
switch(type) {
case ADD_ENTRY:
return {
...state,
[entryId]: {
...state[entryId],
[entryName]: {
[uniqueEntry]: true
}
}
};
}
Any idea what I'm doing wrong?
If you're trying to add an element to the end of the entryName array you should be doing:
return {
...state,
[entryId]: {
...state[entryId],
[entryName]: [
...state[entryId][entryName],
uniqueEntry
]
}
};
ES6 spread with arrays works like this:
const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const eight = 8;
const newArray = ['stuff', ...array1, 'things', ...array2, ...[7, eight], 9];
console.log(newArray); // ["stuff", 1, 2, 3, "things", 4, 5, 6, 7, 8, 9]
Check out this gist which has an example of something very similar to what you're doing.
I found this set of examples extremely helpful as well. A lot of great stuff in here:
https://github.com/sebmarkbage/ecmascript-rest-spread
Update:
If entryName is initialized to undefined like you say in your comment, you could do this:
return {
...state,
[entryId]: {
...state[entryId],
[entryName]: [
...state[entryId][entryName] || [],
uniqueEntry
]
}
};
I think this is a pretty great example of how painful it can be working with React/redux using a heavily nested data structure. FWIW, it's been recommended to me many times to flatten your state as much as possible.