Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function deepEqual(obj1, obj2) {
- if (obj1 === obj2) {
- return true;
- } else if (isObject(obj1) && isObject(obj2)) {
- if (Object.keys(obj1).length !== Object.keys(obj2).length) { return false; }
- for (var prop in obj1) {
- if (!deepEqual(obj1[prop], obj2[prop])) {
- return false;
- }
- }
- return true;
- }
- // Private
- function isObject(obj) {
- if (typeof obj === "object" && obj != null) {
- return true;
- } else {
- return false;
- }
- }
- }
- function compareValues(key, order='asc') {
- return function(a, b) {
- if(!a.hasOwnProperty(key) ||
- !b.hasOwnProperty(key)) {
- return 0;
- }
- const varA = (typeof a[key] === 'string') ?
- a[key].toUpperCase() : a[key];
- const varB = (typeof b[key] === 'string') ?
- b[key].toUpperCase() : b[key];
- let comparison = 0;
- if (varA > varB) {
- comparison = 1;
- } else if (varA < varB) {
- comparison = -1;
- }
- return (
- (order == 'desc') ?
- (comparison * -1) : comparison
- );
- };
- }
- function createNewArray(button){
- let newArray = arrayPadrao.map(function(element){
- if (deepEqual(element, button)){
- return changeActive(button);
- }
- else{
- return element;
- }
- });
- return newArray;
- function changeActive(obj){
- obj.active = !obj.active;
- return obj;
- }
- }
- let arrayPadrao = [
- {
- "active": false,
- "datetime": "1980-12-05"
- },
- {
- "active": true,
- "datetime": "1991-03-15"
- }
- ];
- let botao = JSON.parse(JSON.stringify(arrayPadrao[0]));
- let arrayObjetos = JSON.parse(JSON.stringify(arrayPadrao));
- console.log("antes do click");
- console.log(arrayObjetos);
- console.log("botao");
- console.log(botao);
- console.log("depois do primeiro click");
- arrayObjetos = createNewArray(botao);
- console.log(arrayObjetos);
- console.log("botao");
- console.log(botao);
- console.log("depois do segundo click")
- arrayObjetos = createNewArray(botao);
- console.log(arrayObjetos);
- console.log("botao");
- console.log(botao);
- console.log("array em ordem");
- console.log(arrayObjetos.sort(compareValues('datetime'),'asc'));
- if (!deepEqual(arrayObjetos, arrayPadrao)){
- console.log("ativar botao");
- }
- else{
- console.log("desativar botao");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement