Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

javascript - 如何在react-native中过滤JavaScript对象(How to filter a JavaScript object in react-native)

I want to show a list of user uids and some information about them, except for the information of a specific user.(我想显示用户uid的列表以及有关它们的一些信息,但特定用户的信息除外。)

Here is an example of my JavaScript object, which I am storing in as a usersData state:(这是我的JavaScript对象的示例,我将其存储为usersData状态:) { "alkfklnj2340": { "uid": "alkfklnj2340", "username": "username1" }, "235o2hifoiid": { "uid": "235o2hifoiid", "username": "username2" } } For example, I want to return a new object without alkfklnj2340 , so:(例如,我想返回一个没有alkfklnj2340的新对象,因此:) { "235o2hifoiid": { "uid": "235o2hifoiid", "username": "username2" } } I have tred this so far, which doesn't quite output what is expected:(到目前为止,我已经尝试过了,它并没有输出预期的结果:) const filteredUsers = Object.keys(this.state.usersData).filter((key, i) => this.state.usersData[key].uid != this.state.userUid );   ask by Robert Berge translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use a for...in loop(您可以使用for...in循环)

let result = {}; for (const key in this.state.usersData) { if (key !== this.state.userUid) { result[key] = this.state.usersData[key]; } } or a forEach loop:(或forEach循环:) let result = {}; Object.keys(this.state.usersData).forEach(key => { if (key !== this.state.userUid) { result[key] = this.state.usersData[key]; } });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...