将本机 concat 字符串响应到 json 对象



我想通过附加字符串作为数据键来访问 Json 数据。
下面的代码:

export default class LoginScreen extends Component {
static navigationOptions = {
header: null
}
constructor(props){
super(props);
this.state = {
username:'',
password:'',
data:null
} 
}
componentDidMount(){
fetch("https://api.jsonbin.io/b/5df5c95e0bbce135bb51e276")
.then(response => response.json())
.then((res)=> {    
this.setState({data:res})
})
}
signin_btn=()=>{
var usernameInput = this.state.username;
console.warn(usernameInput);
console.warn(this.state.data.users.concat({usernameInput}));
}

例如:
如果用户名是"ABC">
那么我想要

this.state.data.users.ABC

在对象中设置动态键很容易。使用[]运算符在对象中设置动态键。

let obj = {
a: 1,
b: 2
};
let dynamicKey = 'newKey'
obj[dynamicKey] = 3;
console.log(obj) // {a: 1, b: 2, dynamicKey: 3}

最新更新