使用javascript/typescript对对象中的所有数组进行排序
我想对这个对象中的每个数组进行排序,目前我是这样做的使用javascript/typescript对对象中的所有数组进行排序,javascript,arrays,sorting,typescript,Javascript,Arrays,Sorting,Typescript,我想对这个对象中的每个数组进行排序,目前我是这样做的 this.userInfo = { Interests: [], Websites: [], Games: [], Locations: [], Companies: [], Projects: [], Schools: [], Studies: [] }; this.userInfo.Interests.sort(); this.userInfo.Websites.sort
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();
还有其他更优雅的方法吗?用于获取所有数组自己的属性名,然后使用以下方法迭代键:
试试这个这会帮你的
this.userInfo={
利息:[2,3,4,1],
网站:[2,3,4,1],
游戏:[2,3,4,1],
地点:[2,3,4,1],
公司:[2,3,4,1],
项目:[2,3,4,1],
学校:[2,3,4,1],
研究:[2,3,4,1]
};
for(此.userInfo中的变量i){
console.log(this.userInfo[i].sort());
}
为(this.userInfo中的var prop)this.userInfo[prop].sort()执行一次排序;
Object.keys(this.userInfo).forEach(function(key) {
this.userInfo[key].sort(function(a, b) {
// your sorting logic
});
});