I'll-Be-Back
I'll-Be-Back

Reputation: 10828

Issue with Matching Id between two objects

Why param has been generated too many objects when it should be 4 objects max. It should match Id between dbData.Items[dbItemIndex].Id == lineId and store meta data in param including dbData array Index.

const dbData = {
    Items: [
        {Id: 111},
        {Id: 222},
        {Id: 333},
        {Id: 111},
    ]
}

const sentPayload = {
    Lines: [
        {LineId: 111},
        {LineId: 222},
        {LineId: 333},
        {LineId: 111},
    ]
}

function updateDbSent() {
    const param = [];

    sentPayload.Lines.forEach((line) => {
        let lineId = line.LineId

        for (const dbItemIndex in dbData.Items) {
            if (dbData.Items[dbItemIndex].Id == lineId) {
                param.push({
                    Index: dbItemIndex,
                    Sent: true,
                    Id: lineId,
                })
            }
        }
    });
    
    //update db

    console.log(param);
}

updateDbSent()

I expected to be:

[
  {
    "Index": "0",
    "Sent": true,
    "Id": 111
  },
  {
    "Index": "1",
    "Sent": true,
    "Id": 222
  },
  {
    "Index": "2",
    "Sent": true,
    "Id": 333
  },
  {
    "Index": "3",
    "Sent": true,
    "Id": 111
  }
]

Upvotes: 0

Views: 63

Answers (3)

duc mai
duc mai

Reputation: 1422

Your solution looks a little bit complicated. I would suggest a solution which leverage reduce and find index as following

const dbData = {
 Items: [{ Id: 111 }, { Id: 222 }, { Id: 333 }, { Id: 111 }],
};

const sentPayload = {
  Lines: [{ LineId: 111 }, { LineId: 222 }, { LineId: 333 }, { LineId: 111 }],
};

update the implementation after the author pointed out that my solution did not work with duplicated Id. I updated the solution to use reduce and make index and id combination as key instead

function updateDbSent() {
  const result = sentPayload.Lines.reduce((acc, line, lineIndex) => {
    const { LineId } = line;
    const Index = dbData.Items.findIndex(
      (item, itemIndex) => item.Id === LineId && !acc[`${line} - ${itemIndex}`]
    );
    acc[`${line} - ${Index}`] = {
      Index,
      Id: LineId,
      Sent: true,
    };
    return acc;
  }, {});
  return Object.values(result);
  //update db
}

console.log(updateDbSent());

Upvotes: 1

D Mishra
D Mishra

Reputation: 1578

Use break in for loop and accumulator in foreach and push accumulator

const dbData = {
        Items: [
            { Id: 111 },
            { Id: 222 },
            { Id: 333 },
            { Id: 111 },
        ]
    }
    const sentPayload = {
        Lines: [
            { LineId: 111 },
            { LineId: 222 },
            { LineId: 333 },
            { LineId: 111 },
        ]
    }
    function updateDbSent() {
        const param = [];
        sentPayload.Lines.forEach((line, accumulator ) => {
            let lineId = line.LineId;
            for (const dbItemIndex in dbData.Items) {
                if (dbData.Items[dbItemIndex].Id == lineId) {
                    param.push({
                        Index: accumulator ,
                        Sent: true,
                        Id: lineId,
                    });
                    break;
                }
            }
        });
        //update db
        console.log(param);
    }

    updateDbSent()

Upvotes: 0

Abhas Tandon
Abhas Tandon

Reputation: 1889

You can remove duplicates from your client sentPayload to get the correct output.

Currently, the same payload ids are being checked twice in the DB (In this case 1111) To remove duplicates, you can use a Set.

const lineIds = new Set();
sentPayload.Lines.forEach(lineIdObj => lineIds.add(lineIdObj.LineId))

Now simply loop over lineIds as you were doing in your current code.

function updateDbSent() {
    const param = [];

    lineIds.forEach((lineId) => {

        for (const dbItemIndex in dbData.Items) {
            if (dbData.Items[dbItemIndex].Id == lineId) {
                param.push({
                    Index: dbItemIndex,
                    Sent: true,
                    Id: lineId,
                })
            }
        }
    });

    //update db

    console.log(param);
}

Upvotes: 1

Related Questions