I have 2 array of objects and 2 if conditions
if(JSON.stringify(this.updatedData) !== JSON.stringify(this.originalData) && this.updatedData.length === this.originalData.length) {
this.customAttributes = editedCustomAttributes;
this.customAttributes.forEach(function (element) {
element.action = "edit";
});
}
if (JSON.stringify(this.updatedData) !== JSON.stringify(this.originalData) && his.updatedData.length !== this.originalData.length) {
const items = this.updatedData.filter(item => item.attributeId == null);
items.forEach(function (element) {
element.action = "add";
});
}
While editing a grid row, originalData and updatedData will have the same length hence the first if condition executes. While I am adding new row, updatedData length will be more than originalData hence the 2nd if condition executes.
But I have a situation where a user can add a new row and edit an existing row at once. At that particular scenario, I need to execute both the if conditions and I stuck there. Any suggestions will be appretiated. Thanks.
You could try something as follows:
function check() {
const original = JSON.stringify(originalData)
if (originalData.length !== updatedData.length) {
// If lengths are not equal then new rows were added
addedAction()
// Check the two arrays again without those new rows
const newRows = updatedData.length - originalData.length
if (JSON.stringify(updatedData.slice(0, -newRows)) !== original) {
editedAction()
}
} else if (JSON.stringify(updatedData) !== original) {
editedAction()
}
}
The addedAction() and editedAction() functions are just what will happen in response to an edit or addition.
In the case where an addition and an edit is performed addedAction() will be executed first due to diffirence in lengths. Then originalData will be checked against a portion of the updatedData and the editedAction() function will be executed if they are not the same after removing the newly added rows.
Here is an example with the three cases:
function check() {
const original = JSON.stringify(originalData)
if (originalData.length !== updatedData.length) {
// If lengths are not equal then new rows were added
addedAction()
// Check the two arrays again without those new rows
const newRows = updatedData.length - originalData.length
if (JSON.stringify(updatedData.slice(0, -newRows)) !== original) {
editedAction()
}
} else if (JSON.stringify(updatedData) !== original) {
editedAction()
}
console.log('')
}
function editedAction() {
console.log('Edited')
}
function addedAction() {
console.log('Added')
}
console.log('First case:')
let originalData = [{ name: 'a' }, { name: 'b' }]
let updatedData = [{ name: 'a' }, { name: 'b' }, { name: 'd' }, { name: 'asd' }]
check()
console.log('Second case:')
originalData = [{ name: 'a' }, { name: 'b' }]
updatedData = [{ name: 'a' }, { name: 'TEST' }]
check()
console.log('Third case:')
originalData = [{ name: 'a' }, { name: 'b' }]
updatedData = [{ name: 'a' }, { name: '123' }, { name: 'test2' }]
check()
.as-console-wrapper { min-height: 100% }