Javascript 如果存在多条记录,nodejs将从每个键的已排序json中删除最后一个元素

Javascript 如果存在多条记录,nodejs将从每个键的已排序json中删除最后一个元素,javascript,json,node.js,lodash,Javascript,Json,Node.js,Lodash,我有一个按降序(pid、类型、年份)排序的json,如下所示 [{ "pId" : "60B15", "info" : { "type" : "NT", "year" : 2018 } }, { "pId" : "60B15", "info" : { "type" : "NT", "year" : 2017 } }, { "pId" : "60B15",

我有一个按降序(pid、类型、年份)排序的json,如下所示

[{
    "pId" : "60B15",
    "info" : {      
        "type" : "NT",
        "year" : 2018
    }
},
{
    "pId" : "60B15",
    "info" : {      
        "type" : "NT",
        "year" : 2017
    }
},
{
    "pId" : "60B15",
    "info" : {      
        "type" : "MT",
        "year" : 2017
    }
},
{
    "pId" : "59B15",
    "info" : {      
        "type" : "NT",
        "year" : 2018
    }
},
{
    "pId" : "59B15",
    "info" : {      
        "type" : "MT",
        "year" : 2018
    }
},
{
    "pId" : "59B15",
    "info" : {      
        "type" : "MT",
        "year" : 2017
    }
}]
现在,如果存在多个类型,我想删除每个不同pId的最后一条记录,如果存在单个类型,例如pId 60B15,则存在单个类型MT,我不想删除它。需要下面的输出

[{
    "pId" : "60B15",
    "info" : {      
        "type" : "NT",
        "year" : 2018
    }
},
{
    "pId" : "60B15",
    "info" : {      
        "type" : "MT",
        "year" : 2017
    }
},
{
    "pId" : "59B15",
    "info" : {      
        "type" : "NT",
        "year" : 2018
    }
},
{
    "pId" : "59B15",
    "info" : {      
        "type" : "MT",
        "year" : 2018
    }
}]

如果可以由nodejs lodash完成,那么它将非常棒,我希望它动态地存在,不仅有两种类型MT和NT,还有很多类型存在。

此函数将执行与您要求的相同的操作,它将为您提供唯一的pID和类型组合,而不管您有多少种类型

对于每个新项目,它会检查之前的项目中是否存在类型、id组合。如果该项存在,则该标志将变为false,并且该项不会添加到新数组中

function makeUnique(arr){
    var newArr = [];
    for(let item of arr){
       let flag = true; 
       for(let newItem of newArr){
         if(newItem.pId == item.pId && newItem.info.type == item.info.type){
            flag = false;
            break;
         }
       }
       if(flag){
        newArr.push(item);
       }
    }
    return newArr
}

此函数将执行与您要求的相同的操作,它将为您提供唯一的pID和类型组合,而不管您有多少种类型

对于每个新项目,它会检查之前的项目中是否存在类型、id组合。如果该项存在,则该标志将变为false,并且该项不会添加到新数组中

function makeUnique(arr){
    var newArr = [];
    for(let item of arr){
       let flag = true; 
       for(let newItem of newArr){
         if(newItem.pId == item.pId && newItem.info.type == item.info.type){
            flag = false;
            break;
         }
       }
       if(flag){
        newArr.push(item);
       }
    }
    return newArr
}

如果我理解正确:对于pId和类型的每个组合,您将只拥有最新条目(最高年份)

对于排序数组,ECMAScript 6:

var input = [{
    "pId" : "60B15",
    "info" : {
        "type" : "NT",
        "year" : 2018
    }
},
{
    "pId" : "60B15",
    "info" : {
        "type" : "NT",
        "year" : 2017
    }
},
{
    "pId" : "60B15",
    "info" : {
        "type" : "MT",
        "year" : 2017
    }
},
{
    "pId" : "59B15",
    "info" : {
        "type" : "NT",
        "year" : 2018
    }
},
{
    "pId" : "59B15",
    "info" : {
        "type" : "MT",
        "year" : 2018
    }
},
{
    "pId" : "59B15",
    "info" : {
        "type" : "MT",
        "year" : 2017
    }
}];

const result = input.filter((element, index, array) => index == 0 || !(element.pId === array[index-1].pId &&
      element.info.type === array[index-1].info.type));

console.log(result);

如果我理解正确:对于pId和类型的每个组合,您将只拥有最新条目(最高年份)

对于排序数组,ECMAScript 6:

var input = [{
    "pId" : "60B15",
    "info" : {
        "type" : "NT",
        "year" : 2018
    }
},
{
    "pId" : "60B15",
    "info" : {
        "type" : "NT",
        "year" : 2017
    }
},
{
    "pId" : "60B15",
    "info" : {
        "type" : "MT",
        "year" : 2017
    }
},
{
    "pId" : "59B15",
    "info" : {
        "type" : "NT",
        "year" : 2018
    }
},
{
    "pId" : "59B15",
    "info" : {
        "type" : "MT",
        "year" : 2018
    }
},
{
    "pId" : "59B15",
    "info" : {
        "type" : "MT",
        "year" : 2017
    }
}];

const result = input.filter((element, index, array) => index == 0 || !(element.pId === array[index-1].pId &&
      element.info.type === array[index-1].info.type));

console.log(result);

如果我想删除每个月的最低年份,该怎么办combination@JohneDoe我不明白。我的解决方案保留最高年份,因此删除最低年份(以及其他较低年份)。是否只删除最低年份,还是只保留每个组合中的最低年份?删除最高年份year@JohneDoe一种可能是按升序对json进行排序。因此,脚本将以完全相反的方式运行。否则,这段代码也应该实现您想要的功能:
const result=input.filter((元素、索引、数组)=>index==(array.length-1)| |!(element.pId==数组[index+1].pId&&element.info.type==数组[index+1].info.type))(未测试)PS:如果它不起作用,如果您提供想要得到的结果,它可能会有所帮助。如果我想删除每个月的最低年份,该怎么办combination@JohneDoe我不明白。我的解决方案保留最高年份,因此删除最低年份(以及其他较低年份)。是否只删除最低年份,还是只保留每个组合中的最低年份?删除最高年份year@JohneDoe一种可能是按升序对json进行排序。因此,脚本将以完全相反的方式运行。否则,这段代码也应该实现您想要的功能:
const result=input.filter((元素、索引、数组)=>index==(array.length-1)| |!(element.pId==数组[index+1].pId&&element.info.type==数组[index+1].info.type))(未测试)PS:如果它不起作用,如果您提供您想要得到的结果,它可能会有所帮助。如果我想删除每个组合的最低年份,该怎么办?如果我想删除每个组合的最低年份,该怎么办