0

I am getting below response, i wanted to remove all the duplicates object based on lineId and status. I am trying to find all the duplicates with new status in below response.

In the below example, i wanted to remove those objects whose lineId is same but status is New.

i tried below code but unable to filter the expected result.

I appreciate any help on this.

const response = [
    {       
        "line": "Line 2",
        "lineId": "R_X002_WC02",        
        "status": "New"
    },
    {        
        "line": "Line 3",
        "lineId": "R_X002_WC03",
        "status": "New"
    },
    {       
        "line": "Line 2",
        "lineId": "R_X002_WC02",        
        "status": "Submitted"
    },
    {        
        "line": "Line 4",
        "lineId": "R_X002_WC04",
        "status": "New"
    },
    {        
        "line": "Line 4",
        "lineId": "R_X002_WC04",
        "status": "Submitted"
    }
];

Below is my expected Output

const ExpectedOutput = [    
    {        
        "line": "Line 3",
        "lineId": "R_X002_WC03",
        "status": "New"
    },
    {       
        "line": "Line 2",
        "lineId": "R_X002_WC02",        
        "status": "Submitted"
    },
    {        
        "line": "Line 4",
        "lineId": "R_X002_WC04",
        "status": "Submitted"
    }
];

i tried below code

var finalarr = [];
response.forEach((item) => {
        response.filter(i => i.lineId === item.lineId).length > 1
        finalarr.push(item);
      });
3
  • Does this answer your question? Commented Oct 23, 2023 at 6:00
  • @Tommi,i want to filter based on condition, For ex - i wanted to delete only those objects whose lineId is appearing twice in an array but their status is New. Just need to check if lineId is twice and object having different status so keep submited status object and remove new status object. Commented Oct 23, 2023 at 6:28
  • var finalarr = response.filter((item, i) => i === response.findLastIndex(other => other.lineId === item.lineId)) Commented Oct 23, 2023 at 6:49

2 Answers 2

1

const response = [
    {
        "line": "Line 2",
        "lineId": "R_X002_WC02",
        "status": "New"
    },
    {
        "line": "Line 3",
        "lineId": "R_X002_WC03",
        "status": "New"
    },
    {
        "line": "Line 2",
        "lineId": "R_X002_WC02",
        "status": "Submitted"
    },
    {
        "line": "Line 4",
        "lineId": "R_X002_WC04",
        "status": "New"
    },
    {
        "line": "Line 4",
        "lineId": "R_X002_WC04",
        "status": "Submitted"
    }
];
const uniqueResponse = response.reduce((acc, item) => {
    if (!acc[item.lineId] || item.status === "Submitted") {
        acc[item.lineId] = item;
    }
    return acc;
}, {});

const uniqueResponseArray = Object.values(uniqueResponse);

console.log(uniqueResponseArray);

try this it works according to what you want

Sign up to request clarification or add additional context in comments.

4 Comments

@Shilpa Hay!, sorry now i'll got you point w8 for a sec i'll make update in my code. i'll inform you when its done
Almost identical to the more elegant stackoverflow.com/a/51547038/295783
Filter based on lineId and status both.
"Hello @Shilpa" I have updated my answer, and it now produces the desired output you were looking for. Thank you for your patience. Best regards,"
0

One good way will be to convert this array to a new object where you keys are the key of the object and value can be the object itself. This will get rid of any duplicates

var finalarr = {};
response.forEach((item) => {
        if(!finalarr[i.lineId] {
            finalarr[i.lineId] = i;
        }
    
      });

2 Comments

Ur code is retuning object with unique lineId but with New status, i want object with unique and status is submited, plz chk my expected output.
i wanted to include those object whose lineid is not duplicates bt status is New and also those whose lineId is duplicated but status is submitted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.