需要在国家名称 typescript 或 lodash 上对具有两个字符串元素(国家)的对象数组进行排序
Need to sort an array of objects with two string elements (countries) on the country name typescript or lodash
我有一个这样的对象数组:
{
countries: [{
"country_alpha2_code": "PW",
"country_name": "PALAU"
},{
"country_alpha2_code": "US",
"country_name": "UNITED STATES"
}
]
}
我需要做的是只对 country_name 进行排序,不需要两个字符代码,给这个:
{
countries: [
{
"country_alpha2_code": "US",
"country_name": "UNITED STATES"
},{
"country_alpha2_code": "PW",
"country_name": "PALAU"
}
]
}
我使用 lodash 制作了新的对象数组。这将进入下拉菜单。
我先试了这个:
// this.newJSONCountryArr.sort((a,b) => priorityIndex[a.country_name - b.country_alpha2_code]);
没有。那么这个:
this.newJSONCountryArr.sort(this.compare);
console.log('New Country Arr: ', self.newJSONCountryArr);
compare(a: { country_name: string; }, b: { country_name: string; }): number{
if (a.country_name < b.country_name) {
return -1;
}
if (a.country_name > b.country_name) {
return 1;
}
return 0;
}
我真的不需要返回数字我只是想根据我拥有的对象对对象数组进行排序 country_name。
const countriesFromDb = [
{
country_alpha2_code: 'US',
country_name: 'UNITED STATES'
},
{
country_alpha2_code: 'PW',
country_name: 'PALAU'
}
];
const sorted = [...countriesFromDb].sort((a, b) =>
a.country_name.localeCompare(b.country_name)
);
感谢您提供的所有帮助,我要感谢 Andreas 为我指明了正确的方向。通过对他的代码进行一点点修改,这就是我为使其正常工作所做的工作,而且它确实做到了:
this.newJSONCountryArr.sort((a,b) => a.country_name.localeCompare(b.country_name));
这是整个片段
async getCountryOptions(): Promise<boolean> {
const self = this;
let retVal: boolean;
let priorityIndex = { country_name: 1, country_alpha2_code: 2 };
// this.services.getCountries();
const response = await this.services.getCountries();
// console.log('RESPONSE AFTER AWAIT: ', response);
this.countries = this.services.countryList;
console.log('Did we get countries: ', this.countries);
// Now to make a new array for only country code and name
let results = _.map(this.countries, function (res) {
let newResults = { country_name: res.columns[1].paramValue, country_alpha2_code: res.columns[2].paramValue };
// console.log('New Results: ', newResults);
self.newJSONCountryArr.push(newResults);
});
this.newJSONCountryArr.sort((a,b) => a.country_name.localeCompare(b.country_name));
// this.newJSONCountryArr.sort(this.sortCountries);
console.log('New Country Arr: ', self.newJSONCountryArr);
if (this.countries) {
this.meta.countryOptions.push(this.services.countryList);
retVal = true;
} else {
console.log('Whoops! ')
retVal = false;
}
return retVal;
}
sortCountries(): any {
const sorted = [...this.newJSONCountryArr].sort((a, b) =>
a.country_name.localeCompare(b.country_name)
);
return sorted;
}
我有一个这样的对象数组:
{
countries: [{
"country_alpha2_code": "PW",
"country_name": "PALAU"
},{
"country_alpha2_code": "US",
"country_name": "UNITED STATES"
}
]
}
我需要做的是只对 country_name 进行排序,不需要两个字符代码,给这个:
{
countries: [
{
"country_alpha2_code": "US",
"country_name": "UNITED STATES"
},{
"country_alpha2_code": "PW",
"country_name": "PALAU"
}
]
}
我使用 lodash 制作了新的对象数组。这将进入下拉菜单。
我先试了这个:
// this.newJSONCountryArr.sort((a,b) => priorityIndex[a.country_name - b.country_alpha2_code]);
没有。那么这个:
this.newJSONCountryArr.sort(this.compare);
console.log('New Country Arr: ', self.newJSONCountryArr);
compare(a: { country_name: string; }, b: { country_name: string; }): number{
if (a.country_name < b.country_name) {
return -1;
}
if (a.country_name > b.country_name) {
return 1;
}
return 0;
}
我真的不需要返回数字我只是想根据我拥有的对象对对象数组进行排序 country_name。
const countriesFromDb = [
{
country_alpha2_code: 'US',
country_name: 'UNITED STATES'
},
{
country_alpha2_code: 'PW',
country_name: 'PALAU'
}
];
const sorted = [...countriesFromDb].sort((a, b) =>
a.country_name.localeCompare(b.country_name)
);
感谢您提供的所有帮助,我要感谢 Andreas 为我指明了正确的方向。通过对他的代码进行一点点修改,这就是我为使其正常工作所做的工作,而且它确实做到了:
this.newJSONCountryArr.sort((a,b) => a.country_name.localeCompare(b.country_name));
这是整个片段
async getCountryOptions(): Promise<boolean> {
const self = this;
let retVal: boolean;
let priorityIndex = { country_name: 1, country_alpha2_code: 2 };
// this.services.getCountries();
const response = await this.services.getCountries();
// console.log('RESPONSE AFTER AWAIT: ', response);
this.countries = this.services.countryList;
console.log('Did we get countries: ', this.countries);
// Now to make a new array for only country code and name
let results = _.map(this.countries, function (res) {
let newResults = { country_name: res.columns[1].paramValue, country_alpha2_code: res.columns[2].paramValue };
// console.log('New Results: ', newResults);
self.newJSONCountryArr.push(newResults);
});
this.newJSONCountryArr.sort((a,b) => a.country_name.localeCompare(b.country_name));
// this.newJSONCountryArr.sort(this.sortCountries);
console.log('New Country Arr: ', self.newJSONCountryArr);
if (this.countries) {
this.meta.countryOptions.push(this.services.countryList);
retVal = true;
} else {
console.log('Whoops! ')
retVal = false;
}
return retVal;
}
sortCountries(): any {
const sorted = [...this.newJSONCountryArr].sort((a, b) =>
a.country_name.localeCompare(b.country_name)
);
return sorted;
}