简体   繁体   中英

Javascript Array sorting unique object with a key value condition

I have an array

"datawithisNew": [
        {
            "exam_name": "MPSC",
            "isNew": false
        },
        
        {
            "exam_name": "MPSC",
            "isNew": true
        },
        {
            "exam_name": "UPSC",
            "isNew": false
        },
      
        {
            "exam_name": "RAILWAY",
            "isNew": false
        },
        {
            "exam_name": "RAILWAY",
            "isNew": false
        }]

I am trying to get a result with unique exam_name such that the resulted array of objects hold unique exam_name values also if the exam_name has atleast 1 isNew key as true, the resulted object should have the property with isNew true , if not false . The expected result -

"datawithisNew": [
        {
            "exam_name": "MPSC",
            "isNew": true
        },

        {
            "exam_name": "UPSC",
            "isNew": false
        },
        {
            "exam_name": "RAILWAY",
            "isNew": false
        }]

The code i am using is-

 var helper1 = {};
    var result2 = data12.reduce(function(r, o) {
    var key = o.exam_name ;
    
    if(!helper1[key]) {
      helper1[key] = Object.assign({}, o); // create a copy of o
      r.push(helper1[key]);
    } else {
      helper1[key].exam_name_rating += o.exam_name_rating;
    }
    return r;
  }, []);

But this just return the unique exam_name object i also need isNew key if it occurs atleast true atleast once for the exam_name it should have isNew:true as true or else isNew:false

Use JS Array.filter to filter out duplicates

 const datawithisNew = [{ "exam_name": "MPSC", "isNew": false }, { "exam_name": "MPSC", "isNew": true }, { "exam_name": "UPSC", "isNew": false }, { "exam_name": "RAILWAY", "isNew": false }, { "exam_name": "RAILWAY", "isNew": false } ]; const uniqueItems = datawithisNew.filter((exam, index, self) => { return self.findIndex(e => exam.exam_name === e.exam_name) === index }).map(exam => { if (!exam.isNew && datawithisNew.find(e => e.exam_name === exam.exam_name && e.isNew)) { exam.isNew = true; } return exam; }); console.log(uniqueItems);

Call this below function with your datawithisNew as a parameter

     function getUniqueExams(data){
let finalObjMap = { };
for(var i=0;i<data.length;i++){
let currentObj = data[i];
if(finalObjMap[currentObj["exam_name"]])
{
if(!finalObjMap[currentObj["isNew"]] && currentObj["isNew"])
    finalObjMap[currentObj["exam_name"]]["isNew"]=true;
}
else
{
    finalObjMap[currentObj["exam_name"]]={"isNew":currentObj["isNew"]};
}

}
let finalData=[];
Object.keys(finalObjMap).forEach((key)=>{
let tempExamObj  = { "exam_name": key , "isNew": finalObjMap[key]["isNew"]};
finalData.push(tempExamObj);

});
return finalData;
};

    var datawithisNew=[
            {
                "exam_name": "MPSC",
                "isNew": false
            },
            
            {
                "exam_name": "MPSC",
                "isNew": true
            },
            {
                "exam_name": "UPSC",
                "isNew": false
            },
          
            {
                "exam_name": "RAILWAY",
                "isNew": false
            },
            {
                "exam_name": "RAILWAY",
                "isNew": false
            }];
    console.log(getUniqueExams(datawithisNew));

You where not far from the actual solution, adding the line below solves your problem:

helper1[key].isNew ||= o.isNew;

||= will assign a variable or property to a value if the current value is falsy.

If you cannot use the logical OR assignment ( ||= ) operator due to it being a fairly new assignment operator you can use:

if (!helper1[key].isNew) helper1[key].isNew = o.isNew;

 const data12 = [ { "exam_name": "MPSC", "isNew": false }, { "exam_name": "MPSC", "isNew": true }, { "exam_name": "UPSC", "isNew": false }, { "exam_name": "RAILWAY", "isNew": false }, { "exam_name": "RAILWAY", "isNew": false }, ]; var helper1 = {}; var result2 = data12.reduce(function(r, o) { var key = o.exam_name ; if(!helper1[key]) { helper1[key] = Object.assign({}, o); // create a copy of o r.push(helper1[key]); } else { helper1[key].exam_name_rating += o.exam_name_rating; helper1[key].isNew ||= o.isNew; // <- added this line } return r; }, []); console.log(result2);

Since the example data does not contain the exam_name_rating property, it will be either missing, or set to NaN for the result. I assume this property is available within your own data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM