I am working with an array of objects, where each object contains two properties:
{key:count}
When configuring my chart, I need to set the data source in this format:
{meta: "unknown", value: [the count of unknown]},
{meta: "male", value: [the count of male]},
{meta: "female", value: [the count of female]}
For example, if my current array looks like this:
[{"0":"10"}, {"1":"7"}, {"2":"9"}]
, where 0 represents unknown gender, 1 for male, and 2 for female.
Is there a way to automatically set the value in the chart data based on the key from the array in just one line of code?
Edit:
I have already created a method that accomplishes this:
public getKeyValue(data, key) {
for (var i = 0; i < data.length; i++) {
if (data[i].key == key)
return data[i].count;
}
return 0;
}
However, I'm curious if there is a more concise solution similar to LINQ.