function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nick Tang 1Nick Tang 1 

the JavaScript does not work in the lightning component document

Dear all 
I create the expense tracker app
but I found when I delete one record ,the comparison as below does not work 
expenses[i]!==expense 
so it will result in the record can not remove from list and it should have been deleted but always display in the page
anyone have ideas?
in fact I know one method is get the latest list in the controller and set the list in the component
I just do not know why the source does not work but the salesforce publish the not work source in the document
whether only me meet this issue?
----------------------​------------------source----​----------------------​----------------------​----------------------
deleteExpense : function(component, expense, callback) {
// Call the Apex controller and update the view in the callback
var action = component.get("c.deleteExpense");
action.setParams({
"expense": expense
});
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
// Remove only the deleted expense from view
var expenses = component.get("v.expenses");
var items = [];
for (i = 0; i < expenses.length; i++) {
if(expenses[i]!==expense) {
items.push(expenses[i]);
}
}
component.set("v.expenses", items);
// Other client-side logic
}
});
$A.enqueueAction(action);
Best Answer chosen by Nick Tang 1
Alain CabonAlain Cabon
Hi,

If you want to compare the values of object in javascript, you cannot use: if(expenses[i]!==expense) which just compares the references of the objects (their location in memory).

For a deep comparison of object, the trick widely used is to "stringify" the object in JSON (that works because you don't need to compare functions here).

var myexpense JSON.stringify (expense);
var mytest JSON.stringify(expenses[0]);
alert ('myexpense:' + myexpense + ' test:' + mytest);

if (JSON.stringify(expenses[i])  !==  myexpense) {

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

That works if you build the two objects with the same order of variables inside them (most often the case).

Otherwise, you need your own function isEquivalent:  http://adripofjavascript.com/blog/drips/object-equality-in-javascript.html

Alain

All Answers

Alain CabonAlain Cabon
Hi,

If you want to compare the values of object in javascript, you cannot use: if(expenses[i]!==expense) which just compares the references of the objects (their location in memory).

For a deep comparison of object, the trick widely used is to "stringify" the object in JSON (that works because you don't need to compare functions here).

var myexpense JSON.stringify (expense);
var mytest JSON.stringify(expenses[0]);
alert ('myexpense:' + myexpense + ' test:' + mytest);

if (JSON.stringify(expenses[i])  !==  myexpense) {

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

That works if you build the two objects with the same order of variables inside them (most often the case).

Otherwise, you need your own function isEquivalent:  http://adripofjavascript.com/blog/drips/object-equality-in-javascript.html

Alain
This was selected as the best answer
Nick Tang 1Nick Tang 1
Dear Alain
thanks for your reply
it works fine !!
I appreciate it very much !