简体   繁体   English

Angular9字符串到数组

[英]Angular9 string to array

I have a pandas Series that I have converted into JSON for Angular to display in a table.我有一个 pandas 系列,我已将其转换为 JSON 以便 Angular 显示在表格中。 The issue is that the key values are a python list under a string type.问题在于键值是字符串类型下的 python 列表。 How can I convert the key into an array for Angular?如何将密钥转换为 Angular 的数组?

JSON: JSON:

{ 
"result": {
    "('', '100.83.105.90')": 1, 
    "('AS1124 Universiteit van Amsterdam', '145.18.162.122')": 2, 
    "('AS11796 Airstream Communications, LLC', '64.33.197.15')": 1, 
    "('AS16276 OVH SAS', '51.75.201.126')": 1, 
    "('AS209 CenturyLink Communications, LLC', '174.27.155.12')": 1, 
    "('AS22394 Cellco Partnership DBA Verizon Wireless', '174.241.2.88')": 1, 
    "('AS24608 Wind Tre S.p.A.', '37.227.23.201')": 1, 
    "('AS3329 Vodafone-panafon Hellenic Telecommunications Company SA', '5.55.162.202')": 1, 
    "('AS3352 Telefonica De Espana', '80.24.64.41')": 1, 
    "('AS6128 Cablevision Systems Corp.', '69.116.62.88')": 1, 
    "('AS6805 Telefonica Germany', '2.240.20.127')": 1, 
}

In Angular:在 Angular 中:

<table class="table">
    <thead class="thead-dark">
        <tr>
            <th scope="col" >{{selectedGroup}}</th>
            <th scope="col">{{selectedColumn}}</th>
            <th scope="col">Hits</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let item of mvaHits | keyvalue">
            <td>{{item.key[0]}}</td> 
            <td>{{item.key[1]}}</td> 
            <td>{{item.value}}</td>
        </tr>
    </tbody>
</table>

What it looks like:它看起来像什么: 在此处输入图像描述

How can I fix this?我怎样才能解决这个问题?

Appreciate the help:)感谢帮助:)

The best solution would be to export the data from Python in a proper JSON format.最好的解决方案是以适当的 JSON 格式从 Python 导出数据。 You could then parse the array as a real Javascript array using JSON.parse() .然后,您可以使用JSON.parse()将数组解析为真正的 Javascript 数组。

If you can't adjust mvaHits , this should parse the Python array as a Javascript array and let you access the elements in the array.如果您无法调整mvaHits ,这应该将 Python 数组解析为 Javascript 数组,并让您访问数组中的元素。 Note that this wouldn't work in all cases, especially if the strings in your array had commas.请注意,这并非在所有情况下都有效,特别是如果您的数组中的字符串有逗号。 I would recommend not doing these conversions in the HTML for the sake of clarity and cleanliness, but rather when you load mvaHits for the first time.为了清晰和整洁,我建议不要在 HTML 中进行这些转换,而是在第一次加载mvaHits时进行。 But this should work:但这应该有效:

<table class="table">
    <thead class="thead-dark">
        <tr>
            <th scope="col" >{{selectedGroup}}</th>
            <th scope="col">{{selectedColumn}}</th>
            <th scope="col">Hits</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let item of mvaHits | keyvalue">
            <td>{{item.key.slice(1,-1).split(', ').map((s) => s.slice(1,-1))[0]}}</td> 
            <td>{{item.key.slice(1,-1).split(', ').map((s) => s.slice(1,-1))[1]}}</td> 
            <td>{{item.value}}</td>
        </tr>
    </tbody>
</table>

Breaking it down:分解它:

item.key
    .slice(1,-1) // Removes the first and last characters in the string, removing the ()
    .split(', ') // Splits the string into an array using ', ' as the delimiter
    .map((s) => s.slice(1,-1)) // Removes the quotes from the strings in the array
    [0] // access the first element of the new array

Thanks to @zeterain for helping realize split/slice/etc can be applied directly to item.key .感谢@zeterin 帮助实现 split/slice/etc 可以直接应用于item.key

Solution:解决方案:

<table class="table">
    <thead class="thead-dark">
        <tr>
            <th scope="col" >{{selectedGroup}}</th>
            <th scope="col">{{selectedColumn}}</th>
            <th scope="col">Hits</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let item of mvaHits | keyvalue">
            <td>{{item.key | slice:1:-1 | split:", ":0}}</td> 
            <td>{{item.key | slice:1:-1 | split:", ":1}}</td> 
            <td>{{item.value}}</td>
        </tr>
    </tbody>
</table>

String splitting requires generating a custom pipe, instructions can be found here: https://stackoverflow.com/a/45205047/6575456字符串拆分需要生成自定义 pipe,说明可以在这里找到: https://stackoverflow.com/a/45205047/6575456

Slice pipe (default angular): https://angular.io/api/common/SlicePipe切片 pipe(默认角度): https://angular.io/api/common/SlicePipe

I will propose you to use regular expressions in order to parse the keys.我会建议您使用正则表达式来解析键。 In this case is pretty simple.在这种情况下非常简单。 Regular expressions are a really powerful tool.正则表达式是一个非常强大的工具。

const data = { 
  "result": {
    "('', '100.83.105.90')": 1, 
    "('AS1124 Universiteit van Amsterdam', '145.18.162.122')": 2, 
    "('AS11796 Airstream Communications, LLC', '64.33.197.15')": 1, 
    "('AS16276 OVH SAS', '51.75.201.126')": 1, 
    "('AS209 CenturyLink Communications, LLC', '174.27.155.12')": 1, 
    "('AS22394 Cellco Partnership DBA Verizon Wireless', '174.241.2.88')": 1, 
    "('AS24608 Wind Tre S.p.A.', '37.227.23.201')": 1, 
    "('AS3329 Vodafone-panafon Hellenic Telecommunications Company SA', '5.55.162.202')": 1, 
    "('AS3352 Telefonica De Espana', '80.24.64.41')": 1, 
    "('AS6128 Cablevision Systems Corp.', '69.116.62.88')": 1, 
    "('AS6805 Telefonica Germany', '2.240.20.127')": 1, 
  }
};
const regex = /\('(?<asn>.*)', '(?<ip>.*)'\)/;
const mvaHits = [];
for (const key of Object.keys(data.result)) {
  const found = key.match(regex);
  if (found) {
    mvaHits.push({
      asn: found.groups.asn,
      ip: found.groups.ip,
      hits: data.result[key]
    });
  }
}
console.log(mvaHits);

result结果

[ { asn: '', ip: '100.83.105.90', hits: 1 },
  { asn: 'AS1124 Universiteit van Amsterdam',
    ip: '145.18.162.122',
    hits: 2 },
  { asn: 'AS11796 Airstream Communications, LLC',
    ip: '64.33.197.15',
    hits: 1 },
  { asn: 'AS16276 OVH SAS', ip: '51.75.201.126', hits: 1 },
  { asn: 'AS209 CenturyLink Communications, LLC',
    ip: '174.27.155.12',
    hits: 1 },
  { asn: 'AS22394 Cellco Partnership DBA Verizon Wireless',
    ip: '174.241.2.88',
    hits: 1 },
  { asn: 'AS24608 Wind Tre S.p.A.', ip: '37.227.23.201', hits: 1 },
  { asn:
     'AS3329 Vodafone-panafon Hellenic Telecommunications Company SA',
    ip: '5.55.162.202',
    hits: 1 },
  { asn: 'AS3352 Telefonica De Espana',
    ip: '80.24.64.41',
    hits: 1 },
  { asn: 'AS6128 Cablevision Systems Corp.',
    ip: '69.116.62.88',
    hits: 1 },
  { asn: 'AS6805 Telefonica Germany', ip: '2.240.20.127', hits: 1 } ]

html html

<table class="table">
    <thead class="thead-dark">
        <tr>
            <th scope="col" >{{selectedGroup}}</th>
            <th scope="col">{{selectedColumn}}</th>
            <th scope="col">Hits</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let item of mvaHits">
            <td>{{item.asn}}</td> 
            <td>{{item.ip}}</td> 
            <td>{{item.hits}}</td>
        </tr>
    </tbody>
</table>

Although you can add code logic in angular template, as an advice try not abuse of these.尽管您可以在 angular 模板中添加代码逻辑,但建议不要滥用这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM