So I've been interested in using the splice function on my HTMLCollection Object.
<html>
<head>
<title>Collections</title>
</head>
<body>
<div id="list">
<div style="background-color:lightblue">item1</div>
<div>item2</div>
<div style="background-color:lightblue">item3</div>
</div>
<button onclick="BtnColourItem2_Click()">Colour Item2</button>
Lets assume the list was generated by a server side language like ASP.Net
function BtnRemoveItem2_Click()
{
var list = document.getElementById("list");
list[1].style.backgroundColor = "LightBlue"
//list.splice(1,1)//goal colour the second item and recolour the others to white
for(var item in list)
list[item].style.backgroundColor = "white";
}
Now if this was an array the splice method would exist and I could use it.
note: I've accomplished the functionality by pushing the seperate divs into an actual array to splice the one not needed.
I have tried this:
list.trunc = [].splice;
list.trunc(1, 1);
for(item in list)
console.log(item.innerText);
//results were
//item1
//item2
//item3
Gave me no errors but didnt work. So I threw this together:
newList = new Object();
newList.trunc = [].splice;
newList.push = [].push;
newList.push(list[0].innerText);
newList.push(list[1].innerText);
newList.push(list[2].innerText);
newList.splice(1,1);
console.log(newList);
//outputs { 0 : "item1" , 1 : "item3"}
I found that quite cool. So here is my question. How do you structure an object Array-like enough to get the splice function to work without need to use the push or methods used to enter Array items?
var myArrayLike = Object.create(Array.prototype);This will givemyArrayLikeall the methods ofArray