admin管理员组文章数量:1023187
What I am trying to acplish.
Hydrate chart data with my data from firebase
Issue
The chart I am using want's data in this format
data: [
{ value: 15 },
{ value: 10 },
{ value: 12 },
{ value: 11 },
]
I have the this.datasource that populates my listview and then I can get the values I want in the chart, just the wrong format. I don't know how to get the data without the quotes.
var chartArrayFilter = (JSON.stringify(items, ['value']));
console.log = [{"value":"5"},{"value":"9"},{"value":"0"},{"value":"4"}]
My Question
I have tried to hack that array (or string) and remove the double quotes, but I don't thing React Native lets that happen. Am I doing this pletely wrong? How do I get the data I want in that format from a javascript object?
This is the package I am working with
Thanks!
What I am trying to acplish.
Hydrate chart data with my data from firebase
Issue
The chart I am using want's data in this format
data: [
{ value: 15 },
{ value: 10 },
{ value: 12 },
{ value: 11 },
]
I have the this.datasource that populates my listview and then I can get the values I want in the chart, just the wrong format. I don't know how to get the data without the quotes.
var chartArrayFilter = (JSON.stringify(items, ['value']));
console.log = [{"value":"5"},{"value":"9"},{"value":"0"},{"value":"4"}]
My Question
I have tried to hack that array (or string) and remove the double quotes, but I don't thing React Native lets that happen. Am I doing this pletely wrong? How do I get the data I want in that format from a javascript object?
This is the package I am working with https://www.npmjs./package/react-native-charts
Thanks!
Share Improve this question asked Jul 8, 2016 at 2:27 gbland777gbland777 7253 gold badges11 silver badges26 bronze badges1 Answer
Reset to default 2If you don't need to stringify:
It looks to me like you're using JSON.stringify
as a way to remove unnecessary properties from an array of objects. For example, assuming your items
array looks something like this:
items = [
{
value: 5,
foo: 'bar'
},
{
value: 9,
bar: 'baz'
}
];
then calling JSON.stringify
on that will give you "[{"value":5},{"value":9}]"
(notice how everything is, well, stringified).
Perhaps a better approach would be the following:
var chartArrayFilter = items.map(function(item) {
return {value: item.value};
});
which will give you [{value: 5}, {value: 6}]
. Notice nothing is stringified in that structure; it's just an array of objects.
If you DO need to stringify:
If, for whatever reason, you do need to call JSON.stringify
on your items
array to pick the properties you want, you can simply reverse it with
JSON.parse(items)
after you call JSON.stringify
. This will give you [{value: 5}, {value: 6}, ...]
, same as above.
Conclusion
Once you've prepared your items
array in one of the two ways mentioned above, you should be able to painlessly pass it into react-native-charts
!
What I am trying to acplish.
Hydrate chart data with my data from firebase
Issue
The chart I am using want's data in this format
data: [
{ value: 15 },
{ value: 10 },
{ value: 12 },
{ value: 11 },
]
I have the this.datasource that populates my listview and then I can get the values I want in the chart, just the wrong format. I don't know how to get the data without the quotes.
var chartArrayFilter = (JSON.stringify(items, ['value']));
console.log = [{"value":"5"},{"value":"9"},{"value":"0"},{"value":"4"}]
My Question
I have tried to hack that array (or string) and remove the double quotes, but I don't thing React Native lets that happen. Am I doing this pletely wrong? How do I get the data I want in that format from a javascript object?
This is the package I am working with
Thanks!
What I am trying to acplish.
Hydrate chart data with my data from firebase
Issue
The chart I am using want's data in this format
data: [
{ value: 15 },
{ value: 10 },
{ value: 12 },
{ value: 11 },
]
I have the this.datasource that populates my listview and then I can get the values I want in the chart, just the wrong format. I don't know how to get the data without the quotes.
var chartArrayFilter = (JSON.stringify(items, ['value']));
console.log = [{"value":"5"},{"value":"9"},{"value":"0"},{"value":"4"}]
My Question
I have tried to hack that array (or string) and remove the double quotes, but I don't thing React Native lets that happen. Am I doing this pletely wrong? How do I get the data I want in that format from a javascript object?
This is the package I am working with https://www.npmjs./package/react-native-charts
Thanks!
Share Improve this question asked Jul 8, 2016 at 2:27 gbland777gbland777 7253 gold badges11 silver badges26 bronze badges1 Answer
Reset to default 2If you don't need to stringify:
It looks to me like you're using JSON.stringify
as a way to remove unnecessary properties from an array of objects. For example, assuming your items
array looks something like this:
items = [
{
value: 5,
foo: 'bar'
},
{
value: 9,
bar: 'baz'
}
];
then calling JSON.stringify
on that will give you "[{"value":5},{"value":9}]"
(notice how everything is, well, stringified).
Perhaps a better approach would be the following:
var chartArrayFilter = items.map(function(item) {
return {value: item.value};
});
which will give you [{value: 5}, {value: 6}]
. Notice nothing is stringified in that structure; it's just an array of objects.
If you DO need to stringify:
If, for whatever reason, you do need to call JSON.stringify
on your items
array to pick the properties you want, you can simply reverse it with
JSON.parse(items)
after you call JSON.stringify
. This will give you [{value: 5}, {value: 6}, ...]
, same as above.
Conclusion
Once you've prepared your items
array in one of the two ways mentioned above, you should be able to painlessly pass it into react-native-charts
!
本文标签: listviewReact Native javascript object value to arrayStack Overflow
版权声明:本文标题:listview - React Native javascript object value to array - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745597275a2158246.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论