【问题标题】:converting strings to number from json response - *ngFor - Angular 6将字符串从 json 响应转换为数字 - *ngFor - Angular 6
【发布时间】:2019-03-08 07:16:56
【问题描述】:

我正在尝试将一些字符串从 json 响应转换为数字。例如

邮政编码:“92998-3874”至 92998-3874。遇到了这个SO。但不要在失败的地方。然后我需要在我的视图中循环它,但在堆栈闪电战控制台中面临以下错误。

ERROR
Error: Cannot find a differ supporting object '[object Object]' of type 'Clementina DuBuque'. NgFor only supports binding to Iterables such as Arrays.
Unable to display error. Open your browser's console to view.

到目前为止:

export class AppComponent {
  name = 'Angular';
  private _userList = [];
  private _userListAddress:any = [];
  _sampleURL = 'https://jsonplaceholder.typicode.com/users';

  constructor(private _http: HttpClient) { }
  _loadData() {
    this._http.get<any[]>(this._sampleURL).subscribe(data => {
      this._userList = data;
      this._userListAddress = data[0].address.suite;
      console.log(this._userList);

      for (var _address in this._userList) {
       this._userListAddress = this._userList[_address];
        console.log('address', 
        parseInt(this._userListAddress.address.zipcode));

        /* 

        //below snippet is used to convert strings to num

        [].forEach.call(this._userList, function (a, b) {          
          [].forEach.call(Object.keys(a), function (c) {            
            if (!isNaN(this._userList[b][c]))             
              this._userList[b][c] = +this._userList[b][c];
          });
        });
        console.log(this._userList); */
      }
    }
    )
  }
}


<button (click)="_loadData()">Load data</button>

<div *ngFor="let list of _userList">
<ul>
<li>{{list.address.zipcode}}</li>
</ul>

<div *ngFor="let addresList of _userListAddress">
    <ul>
<li>{{addresList.address.zipcode}}</li>
</ul>
</div>
</div>

哪里做错了或建议更好的方法来实现这一点。 非常感谢。

Stack Blitz

【问题讨论】:

  • 能把你做过的stackblitz的链接制作出来吗?
  • @ManirajfromKarur 我已经更新请检查链接
  • 1) 为什么要将字符串转换为数字? 2)你是否意识到这会导致表达式和结果将是一个减法
  • 我必须开始一些其他要求。为此,我需要将其从字符串转换为数字,然后只有我才能得出其他待定要求是否可行的结论。这就是为什么。我必须在我的路线图中实施 contentEditable 概念

标签: json angular loops angular6


【解决方案1】:

如果我们在谈论这个问题:

ERROR
Error: Cannot find a differ supporting object '[object Object]' of type 'Clementina DuBuque'. NgFor only supports binding to Iterables such as Arrays.
Unable to display error. Open your browser's console to view.

就在这里:

for (var _address in this._userList) {

_address - 这里是一个用户对象,例如

"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
  "street": "Kulas Light",
  "suite": "Apt. 556",
  "city": "Gwenborough",
  "zipcode": "92998-3874",
  "geo": {
    "lat": "-37.3159",
    "lng": "81.1496"
  }
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
  "name": "Romaguera-Crona",
  "catchPhrase": "Multi-layered client-server neural-net",
  "bs": "harness real-time e-markets"
}

然后你试图从用户数组中获取用户对象,绕过用户对象作为键,这实际上是错误的。 this._userListAddress = this._userList[_address];

所以这不是对话问题,只需检查您的代码流程即可。

更新

只需替换特殊字符,然后解析:

// ES6
let stringNumber = '123456-123';
let convertedNumber = Number(stringNumber.replace('-',''));
console.log(convertedNumber);
// pure js
let anotherStringNumber = '123456-123';
let anotherConvertedNumber = parseInt(anotherStringNumber.replace('-',''));
console.log(anotherConvertedNumber);

【讨论】:

  • 是的,你完全正确。只有流错了。但我正在寻找将字符串值转换为数字的方法,然后必须在我看来循环..有什么想法吗?
  • 如果您想将 '####-###' 转换为可能的数字,但结果将是 '#######' - 因为数字不能包含特殊除十进制或千位分隔符以外的字符,如 (',' 和 '.')
  • 是的,也很好。
  • Number("123123-123123".replace('-',''))parceInt("123123-123123".replace('-',''))
  • @worstCoder 适用于您的情况吗?
猜你喜欢
  • 2021-12-05
  • 2012-10-14
  • 2016-12-09
  • 1970-01-01
  • 2022-11-19
  • 1970-01-01
  • 2019-07-04
  • 2019-01-16
  • 2020-12-19
相关资源
最近更新 更多