split - 拆分方法不适用于字符串?TypeError:无法读取未定义的属性“拆分”
问题描述
这是我关心的代码:
我不确定为什么我不能split()
在string
. 我通常使用split()
它,它一直运行良好。这是它第一次不工作。为什么?
import React, { Component } from 'react';
import './App.css';
class App extends Component {
constructor(props){
super(props);
this.state = {
data: []
}
}
componentDidMount(){
var url = 'https://restcountries.eu/rest/v2/all';
fetch(url)
.then(data => data.json())
.then(data => data.map((item, index) => {
return item.name + " " + item.flag;
})).then(data => this.setState({data}));
}
render() {
var randItems = [];
var randNum = 0;
for(let i = 0; i < 4; i++){
randNum = Math.floor(Math.random() * this.state.data.length);
randItems.push(this.state.data[randNum]);
}
var item = randItems[0].split(' ');
console.log('typeof item and its value ' + typeof item + ' ' + item);
注意输出是typeof item
,它的值string
是
解决方案
推荐阅读
- angular - 即使使用 ngOnChanges,Angular @Input() 也不会更新父组件中的值
- refactoring - 如果 UI 中的名称已更改,我是否应该将内部引用重命名为功能名称?
- keycloak - 在一个域下的多个前端应用程序之间切换并使用 Okta 作为 Idp 时,Keycloak 重定向闪烁屏幕
- c# - 在 C# .net winforms 中将字典绑定到 ComboBox
- html - 与之前并排的 CSS 列表项
- kotlinpoet - 如何在 KotlinPoet 中制作生成类的方法返回类型?
- javascript - Expo android构建失败
- ruby-on-rails - Rails - `:start_date` 和 `:end_date` 的 DB 级别验证
- azure - Azure RHEL - 未按照 smp_affinity 中的指定分配中断
- python - 神经网络中反向传播的困惑