check if the column value in object A (i.e., "paymentsType") exists as a key in the first element of leArray[0]. If the key exists, you want to override the value in leArray[0] with the value from object A.
// Define the input objects
let objectA = {
"value": "test 1333",
"column": "paymentsTypeNew",
"id": "xyz123"
};
let leArray = [
{
"accountType": "477777",
"activityType": "7888888",
"paymentsType": "3445555paymentsType",
"rowId": "abcd"
},
{
"accountType": "477777",
"activityType": "7888888",
"paymentsType": "3445555paymentsType",
"rowId": "12345"
},
{
"accountType": "477777",
"activityType": "7888888",
"paymentsType": "jahapanna",
"rowId": "xyz123"
}
];
// Find the object in leArray where rowId matches the id in objectA
let matchingObject = leArray.find(item => item.rowId === objectA.id);
if (matchingObject) {
// Check if the column in objectA exists as a key in the matching object
if (matchingObject.hasOwnProperty(objectA.column)) {
// If the key exists, override the value
matchingObject[objectA.column] = objectA.value;
} else {
// If the key doesn't exist, add the new key-value pair
matchingObject[objectA.column] = objectA.value;
}
}
// Output the updated leArray for confirmation
console.log(leArray);
check if the column value in object A (i.e., "paymentsType") exists as a key in the first element of leArray[0]. If the key exists, you want to override the value in leArray[0] with the value from object A.
Reviewed by dasfrogpractice
on
07:59
Rating:
No comments: