Wednesday, 23 August 2017
REMOVE MATCHING ID IN JAVASCRIPT
--------------------------------------------------------------------------------------------------------------------
REMOVE MATCHING ID IN JAVASCRIPT
READ MORE:
https://jsperf.com/remove-duplicate-array-tests
https://stackoverflow.com/questions/15287865/remove-array-element-based-on-object-property
https://lodash.com/docs/4.17.4#findIndex
--------------------------------------------------------------------------------------------------------------------
CODE:
IN THIS ARRAY NANCY IS SAME BUT IN RESULT NANCY SHOW ONE TIME
var names = ['Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl']
// OUTPUT:Nancy,Mike,Matt,Adam,Jenny,Carl
--------------------------------------------------------------------------------------------------------------------
<p id="demo"></p>
<script>
var names = ['Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl']
var uniq = names
.map((name) => {
return {count: 1, name: name}
})
.reduce((a, b) => {
a[b.name] = (a[b.name] || 0) + b.count
return a
}, {})
var sorted = Object.keys(uniq).sort((a, b) => uniq[a] < uniq[b])
document.getElementById("demo").innerHTML =sorted
</script>
--------------------------------------------------------------------------------------------------------------------
BUTTON ARRAY IN STRING WITH APPEND CHILD METHOD
--------------------------------------------------------------------------------------------------------------------
<!-- https://jsfiddle.net/xh61sru9/1/ -->
<div id="TextBox1"></div>
<script>
var eigenschaften = 3;
var eigenschaftsarray = ["b0","b1","b2"];
function buttonClick(e){
if (e.target.tagName.toLowerCase() == "button") {
var btn = e.target;
TextBox1.innerHTML += btn.id
}
}
function dobutton() {
var parent = document.getElementById('TextBox1');
for (i=0; i<eigenschaften; i++){
var btn = document.createElement("button");
btn.id = eigenschaftsarray[i];
btn.innerHTML = eigenschaftsarray[i].split("_").join(" ");
parent.appendChild(btn);
}
parent.addEventListener("click", buttonClick);
}
dobutton();
</script>
--------------------------------------------------------------------------------------------------------------------
READ MORE:
---------------------------------------------------------------------------------------
<div id="TextBox1"></div> <button id="Btn1">BUTTON1</button> <button id="Btn2">BUTTON2</button> <script> var ButtonIdArray=[Btn1,Btn2]; for(var i = 0; i < ButtonIdArray.length; i++){ ButtonIdArray[i].addEventListener("click",ButtonFunctionName,true); } function ButtonFunctionName(event){ TextBox1.innerHTML+=event.target.id } </script> ----------------------------------------------------------------------------------------- ABOVE CODE CHANGE INTO BUTTON STRING
1) ALL BUTTON DIVS UNDER TEXTBOX DIV
2) WRITE BUTTON ID ARRAY WITH STRING
3)ADD EVENT LISTNER LINE CHANGE ButtonIdArray[i] INTO TextBox1
if (event.target.tagName.toLowerCase() == "button")
can change
TextBox1.innerHTML+=event.target.id
if(event.target.id=="TextBox1"){
TextBox1.innerHTML="";
}
----------------------------------------------------------------------------------------- <div id="TextBox1"> <button id="Btn1">BUTTON1</button> <button id="Btn2">BUTTON2</button> </div> <script> var ButtonIdArray = ["Btn1","Btn2"]; for(var i = 0; i < ButtonIdArray.length; i++){ TextBox1.addEventListener("click",ButtonFunctionName,true); } function ButtonFunctionName(event){if (event.target.tagName.toLowerCase() == "button") {
TextBox1.innerHTML+=event.target.id
}
} </script>
-----------------------------------------------------------------------------------------
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment