This is new question that is rewritten using javascript/jQuery of this thread because i can't do this in MySQL directly query.
MySQL Select id from to and exclude beginning and ending id
So question is simple:
I have array that have received from MySQL query into javascript array:
var array = ["m2", "1", "2", "11", "12", "m4", "m3", "m5", "17", "m1"];
And need function that accept parameter from what array value to show. For example:
function showCategories(marker){
}
So in above example if i call showCategories("m2");
i need to get this:
1
2
11
12
If i call showCategories("m5");
i need to get this:
17
I im currently trying to substr and find index of begin "m2" in first example and find last m index (that is m4) and remove other items from array..is there simple way?
var array = ["m2", "1", "2", "11", "12", "m4", "m3", "m5", "17", "m1"];
function showCategories(marker) {
let currentCat = 'n';
return array.reduce((obj, item) => {
if (item.indexOf("m") === -1) {
obj[currentCat].push(item);
} else {
currentCat = item;
obj[currentCat] = [];
}
return obj;
}, {})[marker] || [];
}
console.log(showCategories("m2"));
console.log(showCategories("m5"));
As you can see, I am using reduce and detecting if the value in the item contains an m
which would indicate a new "category".
Then, I stuff all the values following the new category into an array until another category, again containing m
, shows up.
Hope this helps.