如何重命名和删除数组中的多个键?

huangapple go评论147阅读模式
英文:

How do I rename & delete multiple keys in an array?

问题

以下是翻译好的部分:

我正在尝试在React.js中构建一个使用Highcharts(https://api.highcharts.com/highcharts/)的饼图,它仅接受以下格式的饼图数据(或者我可能是错的):示例Fiddle在这里:https://jsfiddle.net/react_user1/e9cbsrdL/1/

  1. data: [
  2. {name: 'abc', y: 10},
  3. {name: 'def', y: 90}
  4. ]

我从我的API获取的数据看起来是这样的:

  1. const counts: [
  2. {
  3. "id": "all",
  4. "type": "all",
  5. "count": 1403
  6. },
  7. {
  8. "id": "bad",
  9. "type": "bad",
  10. "count": 0
  11. },
  12. {
  13. "id": "failed",
  14. "category": false,
  15. "type": "failed",
  16. "count": 58
  17. },
  18. {
  19. "id": "changed",
  20. "category": true,
  21. "type": "changed",
  22. "count": 123
  23. }

所以我在这里尝试实现三件事:

  1. 1. 删除第一个{}其中包含"id": "all"
  2. 2. 将键id重命名为namecount重命名为y
  3. 3. 删除键typecategory及其数据

感谢您提供的任何帮助,即使是可以帮助的部分答案也会感激不尽。

英文:

I am trying to build a pie chart in react js which uses highcharts (https://api.highcharts.com/highcharts/) is accepting only the following format for pie chart data (or maybe I'm wrong): Sample Fiddle here: https://jsfiddle.net/react_user1/e9cbsrdL/1/

  1. data: [
  2. {name: 'abc', y: 10},
  3. {name: 'def', y: 90}
  4. ]

The data I get from my API looks something like this:

  1. const counts:[
  2. {
  3. "id": "all",
  4. "type": "all",
  5. "count": 1403
  6. },
  7. {
  8. "id": "bad",
  9. "type": "bad",
  10. "count": 0
  11. },
  12. {
  13. "id": "failed",
  14. "category": false,
  15. "type": "failed",
  16. "count": 58
  17. },
  18. {
  19. "id": "changed",
  20. "category": true,
  21. "type": "changed",
  22. "count": 123
  23. }

So I am trying to achieve three things here:

  1. 1. Remove the first {}, with the "id": "all"
  2. 2. Rename the key: "id" to name & "count" to y
  3. 3. Remove the keys: "type" & "category" & their data

Thanks for any help you could provide, even a partial answer that can help would be appreciated.

答案1

得分: 5

我认为你可以使用Array.prototype.filter()Array.prototype.map()的组合。

使用filter(),你可以移除你不需要的值 - 在你的情况下是all,然后使用map(),你可以为数组创建一个新的结构。

从上面提到的文档链接中:

filter()方法创建一个新数组,其中包含通过提供的函数实施的测试的所有元素。

map()方法创建一个新数组,其中包含在调用数组中的每个元素上调用提供的函数的结果。

就像这样:

  1. const counts = [
  2. {
  3. "id": "all",
  4. "type": "all",
  5. "count": 1403
  6. },
  7. {
  8. "id": "bad",
  9. "type": "bad",
  10. "count": 0
  11. },
  12. {
  13. "id": "failed",
  14. "category": false,
  15. "type": "failed",
  16. "count": 58
  17. },
  18. {
  19. "id": "changed",
  20. "category": true,
  21. "type": "changed",
  22. "count": 123
  23. }
  24. ];
  25. const result = counts.filter(f => f.id !== 'all')
  26. .map(e => ({ name: e.id, y: e.count }));
  27. console.log(result);

希望这有所帮助!

英文:

I think you can use Array.prototype.filter() and Array.prototype.map() combination.

With filter() you can remove the value what you don't need - in your case all - then with map() you can create a new structure for you array.

From the documentations - link mentioned above:

> The filter() method creates a new array with all elements that pass the test implemented by the provided function.
>
> The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

Just like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const counts = [
  2. {
  3. &quot;id&quot;: &quot;all&quot;,
  4. &quot;type&quot;: &quot;all&quot;,
  5. &quot;count&quot;: 1403
  6. },
  7. {
  8. &quot;id&quot;: &quot;bad&quot;,
  9. &quot;type&quot;: &quot;bad&quot;,
  10. &quot;count&quot;: 0
  11. },
  12. {
  13. &quot;id&quot;: &quot;failed&quot;,
  14. &quot;category&quot;: false,
  15. &quot;type&quot;: &quot;failed&quot;,
  16. &quot;count&quot;: 58
  17. },
  18. {
  19. &quot;id&quot;: &quot;changed&quot;,
  20. &quot;category&quot;: true,
  21. &quot;type&quot;: &quot;changed&quot;,
  22. &quot;count&quot;: 123
  23. }
  24. ];
  25. const result = counts.filter(f =&gt; f.id !== &#39;all&#39;)
  26. .map(e =&gt; ({ name: e.id, y: e.count }));
  27. console.log(result);

<!-- end snippet -->

I hope this helps!

答案2

得分: 1

你可以将数据以数组的形式提供:

  1. series: [{
  2. data: (() => counts.map(
  3. item => [item.id, item.count]
  4. ))()
  5. }]

在线演示: https://jsfiddle.net/BlackLabel/op4s13dm/

英文:

You can also provide data as an array of arrays:

  1. series: [{
  2. data: (() =&gt; counts.map(
  3. item =&gt; [item.id, item.count]
  4. ))()
  5. }]

Live demo: https://jsfiddle.net/BlackLabel/op4s13dm/

答案3

得分: 0

尝试这个

  1. renderarrayobjects = () => {
  2. return this.state.arrayname.map((Arrayelement, Indexvalue) => {
  3. return (
  4. <View style={styles.btnColor}>
  5. <Text style={styles.tagtext}>{Arrayelement}</Text>
  6. <TouchableOpacity
  7. Indexvalue={Indexvalue}
  8. onPress={() => {
  9. this.remove(Arrayelement);
  10. }}>
  11. <Image style={styles.editskill} source={deletarray} />
  12. </TouchableOpacity>
  13. </View>
  14. );
  15. });
  16. };
  17. 索引值是Index value
  18. 这将呈现一个Array项目的列表我们添加了一个图像按下图像将删除Array交叉图像将出现在数组的每个元素之后
  19. removePeople(Arrayelement) {
  20. var array = [...this.state.Arrayelement];
  21. var index = array.indexOf(Arrayelement);
  22. if (index !== -1) {
  23. array.splice(index, 1);
  24. this.setState({Arrayelement: array});
  25. }
  26. }
  27. 这个方法将删除Array对象
  28. 希望有所帮助如有疑问请随时提问
  29. <details>
  30. <summary>英文:</summary>
  31. try this
  32. renderarrayobjects = () =&gt; {
  33. return this.state.arrayname.map((Arrayelement, Indexvalue) =&gt; {
  34. return (
  35. &lt;View style={styles.btnColor}&gt;
  36. &lt;Text style={styles.tagtext}&gt;{Arrayelement}&lt;/Text&gt;
  37. &lt;TouchableOpacity
  38. Indexvalue={Indexvalue}
  39. onPress={() =&gt; {
  40. this.remove(Arrayelement);
  41. }}&gt;
  42. &lt;Image style={styles.editskill} source={deletarray} /&gt;
  43. &lt;/TouchableOpacity&gt;
  44. &lt;/View&gt;
  45. );
  46. }); };
  47. The index value is the Index value.
  48. this will render a list of Array items and we add an image that will be pressed to delete array the cross image will appear after every element of an array
  49. removePeople(Arrayelement) {
  50. var array = [...this.state.Arrayelement];
  51. var index = array.indexOf(Arrayelement);
  52. if (index !== -1) {
  53. array.splice(index, 1);
  54. this.setState({Arrayelement: array});
  55. } }
  56. this method will delete array objects.
  57. Hope it helps .feel free for doubts
  58. </details>

huangapple
  • 本文由 发表于 2020年1月3日 20:15:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定