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:
![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.](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZjafN34-plPbuqbMA3gmD8HDZMivVA_IausUHw-AA8m9auVLAWvrxulHwbRSHGwUmIc34yYbT2kYeYiuo0eTPA5O_UsysTI9k6T-xhpMN6Uzo2GzE4fxLY5WnJUIU23EvtgrEMl0LiD9Yy9Xg15DafLeCcGoU6KSvwIAEP0RVPV3OmvdHxdu9ezhmT66L/s72-c/javascript%20array.png)
No comments: