Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用TypeScript对数组进行排序,但不包括一个项_Typescript - Fatal编程技术网

使用TypeScript对数组进行排序,但不包括一个项

使用TypeScript对数组进行排序,但不包括一个项,typescript,Typescript,我有以下类型脚本代码: export const cgGroups = [ { id: 2, name: 'North America & Caribbean' }, { id: 3, name: 'Latin America' }, { id: 6, name: 'Europe' }, { id: 4, name: 'Asia Pacific' }, { id: 1, n

我有以下类型脚本代码:

export const cgGroups = [
  {
    id: 2,
    name: 'North America & Caribbean'
  },
  {
    id: 3,
    name: 'Latin America'
  },
  {
    id: 6,
    name: 'Europe'
  },
  {
    id: 4,
    name: 'Asia Pacific'
  },
  {
    id: 1,
    name: 'Middle East & Africa'
  },
  {
    id: 7,
    name: 'International'
  }
];
除了一个对象外,我想按字母顺序对上面的内容进行排序

{
   id: 7,
   name: 'International'
}
我想把它移到排序数组的最后一个

我尝试了以下代码进行排序:

cgGroups = cgGroups.map(({id, name}) => ({id, name})).sort((a, b) => {
        if (a.name.toLowerCase() > b.name.toLowerCase()) {
          return 1;
        }
        if (a.name.toLowerCase() < b.name.toLowerCase()) {
          return -1;
        }
        return 0;
      });
cgGroups=cgGroups.map({id,name})=>({id,name})).sort((a,b)=>{
if(a.name.toLowerCase()>b.name.toLowerCase()){
返回1;
}
if(a.name.toLowerCase()
以下是预期输出:

亚太地区、欧洲、拉丁美洲、中东和非洲、北美和加勒比以及国际


有人能在这里指导我解决这个问题吗?

如果您不介意向数组原型添加一个方法,那么有两个可能的解决方案(第一个修改原始数组,而第二个返回新数组)

让cgGroups=[
{
id:2,
名称:“北美和加勒比海”
},
{
id:3,
名称:“拉丁美洲”
},
{
id:6,
名称:“欧洲”
},
{
id:4,
名称:“亚太地区”
},
{
id:1,
名称:“中东和非洲”
},
{
id:7,
名称:“国际”
}
];
常数sortAlph=(a,b)=>{
if(a.name.toLowerCase()>b.name.toLowerCase()){
返回1;
}
if(a.name.toLowerCase()x!==element).concat(element);
}
CGG组
.sort(sortAlph)
.move(cgGroups.findIndex(x=>x.name==='International'),cgGroups.length)
newArr=CGG组
.sort(sortAlph)
.moveToTheEnd(cgGroups.findIndex(x=>x.name==='International'))
控制台日志(cgGroups);

控制台日志(newArr)如果您不介意向数组原型添加一个方法,那么有两种可能的解决方案(第一种修改原始数组,而第二种返回新数组)

让cgGroups=[
{
id:2,
名称:“北美和加勒比海”
},
{
id:3,
名称:“拉丁美洲”
},
{
id:6,
名称:“欧洲”
},
{
id:4,
名称:“亚太地区”
},
{
id:1,
名称:“中东和非洲”
},
{
id:7,
名称:“国际”
}
];
常数sortAlph=(a,b)=>{
if(a.name.toLowerCase()>b.name.toLowerCase()){
返回1;
}
if(a.name.toLowerCase()x!==element).concat(element);
}
CGG组
.sort(sortAlph)
.move(cgGroups.findIndex(x=>x.name==='International'),cgGroups.length)
newArr=CGG组
.sort(sortAlph)
.moveToTheEnd(cgGroups.findIndex(x=>x.name==='International'))
控制台日志(cgGroups);

控制台日志(newArr)
它不起作用,因为您没有使用
名称:'International'
将项目的条件编码到比较函数中

可能是这样的:

cgGroups = cgGroups.map(({id, name}) => ({id, name})).sort((a, b) => {
    if (a.name.toLowerCase() == 'international') {
        return +1;      // "a" is the greatest element of the array
    } else if (b.name.toLowerCase() == 'international') {
        return -1;      // "a" stays before "b" because "b" is the last item
    } else if (a.name.toLowerCase() > b.name.toLowerCase()) {
        return 1;       // regular items, compare their names
    } else if (a.name.toLowerCase() < b.name.toLowerCase()) {
        return -1;
    }

    return 0;
});
cgGroups=cgGroups.map({id,name})=>({id,name})).sort((a,b)=>{
if(a.name.toLowerCase()=='international'){
return+1;/“a”是数组中最大的元素
}else if(b.name.toLowerCase()=='international'){
return-1;/“a”位于“b”之前,因为“b”是最后一项
}else if(a.name.toLowerCase()>b.name.toLowerCase()){
返回1;//常规项,比较它们的名称
}else if(a.name.toLowerCase()
它不起作用,因为您没有在比较函数中使用
名称:'International'
对项目的条件进行编码

可能是这样的:

cgGroups = cgGroups.map(({id, name}) => ({id, name})).sort((a, b) => {
    if (a.name.toLowerCase() == 'international') {
        return +1;      // "a" is the greatest element of the array
    } else if (b.name.toLowerCase() == 'international') {
        return -1;      // "a" stays before "b" because "b" is the last item
    } else if (a.name.toLowerCase() > b.name.toLowerCase()) {
        return 1;       // regular items, compare their names
    } else if (a.name.toLowerCase() < b.name.toLowerCase()) {
        return -1;
    }

    return 0;
});
cgGroups=cgGroups.map({id,name})=>({id,name})).sort((a,b)=>{
if(a.name.toLowerCase()=='international'){
return+1;/“a”是数组中最大的元素
}else if(b.name.toLowerCase()=='international'){
return-1;/“a”位于“b”之前,因为“b”是最后一项
}else if(a.name.toLowerCase()>b.name.toLowerCase()){
返回1;//常规项,比较它们的名称
}else if(a.name.toLowerCase()
If a.name==“International”return-1和其他If语句可以跟在后面。但是你的预期产出没有意义,亚洲应该排在第一位(按名称按字母顺序排序)你说过要按字母顺序排序,亚太地区不应该是第一个条目吗?对不起,我现在已经更正了。非常感谢您指出:)它不起作用,因为您没有将带有
id:7
的项的条件编码到比较函数中。此问题中没有涉及TypeScript。这是纯JavaScript,您发布的代码在浏览器中工作。但是你的预期产出没有意义,亚洲应该排在第一位(按名称按字母顺序排序)你说过要按字母顺序排序,亚太地区不应该是第一个条目吗?对不起,我现在已经更正了。非常感谢您指出:)它不起作用,因为您没有将带有
id:7
的项的条件编码到比较函数中。此问题中没有涉及TypeScript。它是纯净的