[英]is there a way in JavaScript/TypeScript of adding two arrays in an object and sort them?
[英]Sort all arrays in object using javascript/typescript
我想对这个对象中的每个数组进行排序,目前我是以这种方式进行的
this.userInfo = {
Interests: [],
Websites: [],
Games: [],
Locations: [],
Companies: [],
Projects: [],
Schools: [],
Studies: []
};
this.userInfo.Interests.sort();
this.userInfo.Websites.sort();
this.userInfo.Games.sort();
this.userInfo.Locations.sort();
this.userInfo.Companies.sort();
this.userInfo.Projects.sort();
this.userInfo.Schools.sort();
this.userInfo.Studies.sort();
还有其他更优雅的方式吗?
使用Object.keys
获取所有数组自身的属性名称,然后使用Array#forEach
迭代键:
Object.keys(this.userInfo).forEach(function(key) {
this.userInfo[key].sort(function(a, b) {
// your sorting logic
});
});
试试这个,对您有帮助,
this.userInfo = { Interests: [2, 3, 4, 1], Websites: [2, 3, 4, 1], Games: [2, 3, 4, 1], Locations: [2, 3, 4, 1], Companies: [2, 3, 4, 1], Projects: [2, 3, 4, 1], Schools: [2, 3, 4, 1], Studies: [2, 3, 4, 1] }; for (var i in this.userInfo) { console.log(this.userInfo[i].sort()); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.