最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

listview - React Native javascript object value to array - Stack Overflow

matteradmin7PV0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 2

If 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!

Post a comment

comment list (0)

  1. No comments so far