0

I have a tricky requirement which im not able to fix.

I have a button and if i click on that button, Im getting below data from a REST web service ..

{
"results": [{
            "Reqdate": "\/Date(1520899200000)\/",
            "Tooltip": "13.03.2018 Blood Donation Approved ",
            "Legendid": "GROUP_LEVEL1"
            },{
            "Reqdate": "\/Date(1523836800000)\/",
            "Tooltip": "16.04.2018 Privilege Leave Sent ",
            "Legendid": "BADVALUE_LIGHT",
            },{
            "Reqdate": "\/Date(1524528000000)\/",
            "Tooltip": "24.04.2018 Privilege Leave Sent ",
            "Legendid": "BADVALUE_LIGHT",
            }]

            }

If im getting the above data for the first time. Im going to display one section. Now again if i click on the same button, If i get the same data in the service then i have to hide the section. But im not able to check the same data for the second time. Im trying to fix this issue by storing the data in a variable like below..

var firstTimeResults = data.results;

Now im not able to check if the same data is coming for the second time. Actually im not able to produce the sample code too. Im sorry for that

Can someone please help me to fix this issue.

user2644620
  • 199
  • 1
  • 10
  • 37

2 Answers2

1

I am not fully understand your problem but I think you can store your data as JSON and check if the same data is return from the second time onwards?

Code example

// Variable declaration
var firstTimeResults = null;

// When data return
var dataJson = JSON.stringify(data.results);
if (firstTimeResults === null) {
 firstTimeResults = dataJson;
}
else {
 // check to see if the data is the same as the firstTimeResults
 var isSame = firstTimeResults === dataJson;
 if (isSame) {
    // Same
 } else {
    // Not the same
 }
}
Robert Chan
  • 144
  • 11
0

You can simply try with Lodash library. It makes JavaScript easier to work with arrays, numbers, objects, strings, etc.

https://lodash.com/

var firstResult = null;
var secondResult;
var showSection;

function getData() {
  // here goes your API request to get the data        
};

function onButtonClick() {
   this.getData().then(function(value) {

      if !(firstResult) {
        firstResult = value;
        showSection = true;

        return;
      }

      secondResult = value;

      if (_.isEqual(firstResult, secondResult)) {
          showSection = false;
      } 
   })
};

Hope this will be helpful to you!

Dilushika Weerawardhana
  • 1,441
  • 1
  • 12
  • 16