JSFiddle - React, Tailwind, and code Playground
by Tika Datta Pahadi
JavaScript
var _array = ["", "Løse kugler", "Kugletæpper", [
"Uldprodukter", "Bordskånere", ["Stolehynder", "6"]]]
/*
@args : array and string: name of item
@return : string : index (eg: 2.3.3)
*/
Array.prototype.findDotIndex = function (item) {
for (var index in this) {
//Testing if item is array
if (this[index] instanceof Array) {
return index + "." + this[index].findDotIndex(item);
} else {
if (item == this[index]) {
return index;
}
}
}
}
/*
@args : array and index: (eg: 2.3.3)
@return : string || array
*/
Array.prototype.getItemFromDotIndex = function(index) {
index = index.toString().split('.');
//Making sure it's string and splitting
var temp = this;
for(var i = 0; i < index.length; i++){
temp = temp[index[i]];
}
return temp;
}
console.log(_array.findDotIndex('Stolehynder'));
console.log(_array.getItemFromDotIndex('3.2'));