JSFiddle - React, Tailwind, and code Playground
by csornmo
HTML
<button>Apple</button>
<button>Banana</button>
<button>Strawberry</button>
<button>Lime</button>
JavaScript
var arr = [];
var uniqueArr = [];
$('button').click(function() {
var fruit = $(this).text();
arr.push(fruit);
uniqueArr.push(fruit);
$.each(arr, function(i, el) {
if ($.inArray(el, uniqueArr) === -1) {
if (uniqueArr.length > 3) {
uniqueArr.slice(i);
}
} else {
if (uniqueArr.length > 3) {
uniqueArr.shift(i);
}
}
});
console.log(uniqueArr);
// Save
//localStorage.setItem("datas", JSON.stringify(arr));
// Retrieve
//var stored_datas = JSON.parse(localStorage.getItem('datas'));
// console.log(stored_datas);
});