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

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

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/

data: [
      {name: 'abc', y: 10},
      {name: 'def', y: 90}
]

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

const counts: [
{
"id": "all",
"type": "all",
"count": 1403
},
{
"id": "bad",
"type": "bad",
"count": 0
},
{
"id": "failed",
"category": false,
"type": "failed",
"count": 58
},
{
"id": "changed",
"category": true,
"type": "changed",
"count": 123
}

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

1. 删除第一个{}其中包含"id": "all"
2. 将键id重命名为namecount重命名为y
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/

data: [
      {name: 'abc', y: 10},
      {name: 'def', y: 90}
]

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

const counts:[
{
"id": "all",
"type": "all",
"count": 1403
},
{
"id": "bad",
"type": "bad",
"count": 0
},
{
"id": "failed",
"category": false,
"type": "failed",
"count": 58
},
{
"id": "changed",
"category": true,
"type": "changed",
"count": 123
}

So I am trying to achieve three things here:

1. Remove the first {}, with the "id": "all"
2. Rename the key: "id" to name & "count" to y
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()方法创建一个新数组,其中包含在调用数组中的每个元素上调用提供的函数的结果。

就像这样:

const counts = [
  {
    "id": "all",
    "type": "all",
    "count": 1403
  },
  {
    "id": "bad",
    "type": "bad",
    "count": 0
  },
  {
    "id": "failed",
    "category": false,
    "type": "failed",
    "count": 58
  },
  {
    "id": "changed",
    "category": true,
    "type": "changed",
    "count": 123
  }
];

const result = counts.filter(f => f.id !== 'all')
                     .map(e => ({ name: e.id, y: e.count }));

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

const counts = [
  {
  &quot;id&quot;: &quot;all&quot;,
  &quot;type&quot;: &quot;all&quot;,
  &quot;count&quot;: 1403
  },
  {
  &quot;id&quot;: &quot;bad&quot;,
  &quot;type&quot;: &quot;bad&quot;,
  &quot;count&quot;: 0
  },
  {
  &quot;id&quot;: &quot;failed&quot;,
  &quot;category&quot;: false,
  &quot;type&quot;: &quot;failed&quot;,
  &quot;count&quot;: 58
  },
  {
  &quot;id&quot;: &quot;changed&quot;,
  &quot;category&quot;: true,
  &quot;type&quot;: &quot;changed&quot;,
  &quot;count&quot;: 123
  }
];

const result = counts.filter(f =&gt; f.id !== &#39;all&#39;)
                     .map(e =&gt; ({ name: e.id, y: e.count }));

console.log(result);

<!-- end snippet -->

I hope this helps!

答案2

得分: 1

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

series: [{
    data: (() => counts.map(
        item => [item.id, item.count]
    ))()
}]

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

英文:

You can also provide data as an array of arrays:

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

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

答案3

得分: 0

尝试这个

renderarrayobjects = () => {
  return this.state.arrayname.map((Arrayelement, Indexvalue) => {
    return (
      <View style={styles.btnColor}>
        <Text style={styles.tagtext}>{Arrayelement}</Text>
        <TouchableOpacity
          Indexvalue={Indexvalue}
          onPress={() => {
            this.remove(Arrayelement);
          }}>
          <Image style={styles.editskill} source={deletarray} />
        </TouchableOpacity>
      </View>
    );
  });
};

索引值是Index value
这将呈现一个Array项目的列表我们添加了一个图像按下图像将删除Array交叉图像将出现在数组的每个元素之后

removePeople(Arrayelement) {
  var array = [...this.state.Arrayelement];
  var index = array.indexOf(Arrayelement);
  if (index !== -1) {
    array.splice(index, 1);
    this.setState({Arrayelement: array});
  }
}

这个方法将删除Array对象

希望有所帮助如有疑问请随时提问

<details>
<summary>英文:</summary>

try this 

      renderarrayobjects = () =&gt; {
    return this.state.arrayname.map((Arrayelement, Indexvalue) =&gt; {
      return (
        &lt;View style={styles.btnColor}&gt;
          &lt;Text style={styles.tagtext}&gt;{Arrayelement}&lt;/Text&gt;
          &lt;TouchableOpacity
            Indexvalue={Indexvalue}
            onPress={() =&gt; {
              this.remove(Arrayelement);
            }}&gt;
            &lt;Image style={styles.editskill} source={deletarray} /&gt;
          &lt;/TouchableOpacity&gt;
        &lt;/View&gt;
      );
    }); };

The index value is the Index value.
   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

      removePeople(Arrayelement) {
    var array = [...this.state.Arrayelement];
    var index = array.indexOf(Arrayelement);
    if (index !== -1) {
      array.splice(index, 1);
      this.setState({Arrayelement: array});
    }  }


this method will delete array objects.

Hope it helps .feel free for doubts



</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:

确定