JSFiddle - React, Tailwind, and code Playground
by Trisox
JavaScript
var test = ['a', 'b', 'c', 'd'];
var routeConfig = test.reverse();
//check localstoge
var localStorage = ['d', null, 'b', null];
var result = [];
//loop through all the routeConfig's
for (i = 0; i < routeConfig.length; i++) {
//check if localstorage and routeconfig have the same size/length
if (localStorage.length == routeConfig.length) {
//converts a localStorage[i] to a boolean and ensures a boolean type for checking
if (!!localStorage[i]) {
//if localStorage is not there hence -1 or || i is bigger then the index of the current pushed result
if (localStorage.indexOf(result[0]) === -1 || i >= localStorage.indexOf(result[0])) {
// empty the array result
result = [];
//push the new result in to the array
result.push(localStorage[i]);
}
}
}
}
console.log(result, 'result');
//reverse loop
//check if array is same size;
//loop untill first item is defined;
//if first item is defined push array item to array;
//if othere items in array are undefined that have a smaller index then current one
//clean array and push new item to array;
// in the end return item from array that can be only one item.