below is my function
getUpdatedGridData(array) {
return Object.values(array.reduce((acc, { attributeId, ...rest }) => {
acc[attributeId] = acc[attributeId] || {};
acc[attributeId].attributeId = attributeId;
Object.entries(rest).forEach(([key, value]) => {
if (acc[attributeId][key] && value !== '') {
acc[attributeId][key] = value;
}
if (!acc[attributeId][key]) {
acc[attributeId][key] = value;
}
});
return acc;
}, {}));
}
Example array parameter which is sent to the above method
Array(2)
--------
0: {attributeId: 2, objectType: "OPERATORS", attributeName: "PYUSERNAME"}
1: {attributeId: 1, objectType: "PORTFOLIO", attributeName: "PORTFOLIONAME"}
As we can see in 0th position we have attributeId 2 and at 1st index we have attributeId 1. So, when I receive the updated data from above method, the position of the indexes are getting changed. For the above request, at 0th position I am getting attrId 1 and at 1st position I am getting attrId 2. I am not really able to understand how to modify the above method to keep the indexes properly. Please advice. Thanks