1

I have this array of objects I have tried to remove a duplicate from the array, but I can't remove it based on a condition this is the array

result = [
 { jobId: 2, requiredNumber: 2, acceptableRate: 80 },
 { jobId: 3, requiredNumber: 3, acceptableRate: 80 },
 { jobId: 4, requiredNumber: 1, acceptableRate: 80 },
 { jobId: 2, requiredNumber: 1, acceptableRate: 95 } 
]

what I have done is using the .filter() and the .findIndex() but I can't get my excepted output this is what I have done

var mm = result.filter(
 (value, index, self) =>
 index === self.findIndex((t) => t.jobId === value.jobId)
);
console.log(mm);

my output was

[
 { jobId: 2, requiredNumber: 2, acceptableRate: 80 },
 { jobId: 3, requiredNumber: 3, acceptableRate: 80 },
 { jobId: 4, requiredNumber: 1, acceptableRate: 80 }
]

expected output is based on my condition which can be based on higher acceptableRate or higher requiredNumber

[
 { jobId: 2, requiredNumber: 1, acceptableRate: 95 },
 { jobId: 3, requiredNumber: 3, acceptableRate: 80 },
 { jobId: 4, requiredNumber: 1, acceptableRate: 80 },
]
Bergi
671k162 gold badges1k silver badges1.5k bronze badges
asked Oct 8, 2022 at 23:23
3
  • 1
    Define "duplicate", I don't understand your expected output. Commented Oct 8, 2022 at 23:29
  • From the above question it seems to remove duplicate JobID record which have lower acceptable rate in case of equal acceptable rate in jobID remove lower required number record. Commented Oct 8, 2022 at 23:35
  • @JitendraPathak yes that is correct Commented Oct 8, 2022 at 23:38

3 Answers 3

2

First sort the array by acceptableRate or if they are the same values by requiredNumber then find duplicates. This way.

result = [
 { jobId: 2, requiredNumber: 2, acceptableRate: 80 },
 { jobId: 3, requiredNumber: 3, acceptableRate: 80 },
 { jobId: 4, requiredNumber: 1, acceptableRate: 80 },
 { jobId: 2, requiredNumber: 1, acceptableRate: 95 },
];
 result.sort(function (a, b) {
 return b.acceptableRate - a.acceptableRate || b.requiredNumber - a.requiredNumber;
 });
var mm = result.filter(
 (value, index, self) =>
 index === self.findIndex((t) => t.jobId === value.jobId),
);
console.log(mm);

answered Oct 8, 2022 at 23:38
Sign up to request clarification or add additional context in comments.

Comments

0

The following code will filter the values ​​for you if the jobId comes first according to acceptableRate, and if there are two identical values ​​then it will filter according to requiredNumber

var mm = result.sort((a, b) => b.jobId < a.jobId ? true : b.jobId > a.jobId ? false : b.acceptableRate > a.acceptableRate ? true : b.acceptableRate < a.acceptableRate ? false : b.requiredNumber - a.requiredNumber);
answered Oct 8, 2022 at 23:49

Comments

0

If I understood your question right, here might be the code. We filter out all the items if there's an item with same jobId and higher acceptableRate.

const result = [
 { jobId: 2, requiredNumber: 2, acceptableRate: 80 },
 { jobId: 3, requiredNumber: 3, acceptableRate: 80 },
 { jobId: 4, requiredNumber: 1, acceptableRate: 80 },
 { jobId: 2, requiredNumber: 1, acceptableRate: 95 } 
];
const finalResult = result.filter(x => 
 !result.find(r => 
 r.jobId === x.jobId && 
 r.acceptableRate > x.acceptableRate));
console.log(finalResult);

answered Oct 8, 2022 at 23:50

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.