Check if two objects with different structure are identical? [closed] - javascript
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to check if two objects are identical, using Pid and Vid.. I know how to do it in PHP, but JavaScript is so hard for me...
// Object 1
{"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"103646"}},{"#attributes":{"Pid":"5919063","Vid":"6536025"}}]}}
// Object 2 (Pid:Vid,Pid:Vid,Pid:Vid,Pid:Vid)
{"1627207":"38330499","10004":"513661344","20122":"103646","5919063":"6536025"}
EDIT:
I did it this way, but it's really slow...
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var details = JSON.parse('{"ConfiguredItems":{"OtapiConfiguredItem":[{"Id":"3657280986465","Quantity":"8674","Price":{"OriginalPrice":"839.00","MarginPrice":"839","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"839","DisplayedMoneys":{"Money":"128.84"}},"ConvertedPrice":"128.84$","ConvertedPriceWithoutSign":"128.84","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"839.00","MarginPrice":"839","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"839","DisplayedMoneys":{"Money":"128.84"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"103646"}},{"#attributes":{"Pid":"5919063","Vid":"6536025"}}]}},{"Id":"3657280986466","Quantity":"9878","Price":{"OriginalPrice":"869.00","MarginPrice":"869","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"869","DisplayedMoneys":{"Money":"133.45"}},"ConvertedPrice":"133.45$","ConvertedPriceWithoutSign":"133.45","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"869.00","MarginPrice":"869","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"869","DisplayedMoneys":{"Money":"133.45"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"103646"}},{"#attributes":{"Pid":"5919063","Vid":"3266779"}}]}},{"Id":"3657280986467","Quantity":"9989","Price":{"OriginalPrice":"889.00","MarginPrice":"889","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"889","DisplayedMoneys":{"Money":"136.52"}},"ConvertedPrice":"136.52$","ConvertedPriceWithoutSign":"136.52","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"889.00","MarginPrice":"889","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"889","DisplayedMoneys":{"Money":"136.52"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"103646"}},{"#attributes":{"Pid":"5919063","Vid":"3266781"}}]}},{"Id":"3657280986468","Quantity":"9995","Price":{"OriginalPrice":"919.00","MarginPrice":"919","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"919","DisplayedMoneys":{"Money":"141.12"}},"ConvertedPrice":"141.12$","ConvertedPriceWithoutSign":"141.12","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"919.00","MarginPrice":"919","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"919","DisplayedMoneys":{"Money":"141.12"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"103646"}},{"#attributes":{"Pid":"5919063","Vid":"3266785"}}]}},{"Id":"3657280986469","Quantity":"9994","Price":{"OriginalPrice":"959.00","MarginPrice":"959","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"959","DisplayedMoneys":{"Money":"147.27"}},"ConvertedPrice":"147.27$","ConvertedPriceWithoutSign":"147.27","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"959.00","MarginPrice":"959","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"959","DisplayedMoneys":{"Money":"147.27"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"103646"}},{"#attributes":{"Pid":"5919063","Vid":"3266786"}}]}},{"Id":"3657280986470","Quantity":"8993","Price":{"OriginalPrice":"869.00","MarginPrice":"869","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"869","DisplayedMoneys":{"Money":"133.45"}},"ConvertedPrice":"133.45$","ConvertedPriceWithoutSign":"133.45","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"869.00","MarginPrice":"869","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"869","DisplayedMoneys":{"Money":"133.45"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"4209035"}},{"#attributes":{"Pid":"5919063","Vid":"6536025"}}]}},{"Id":"3657280986471","Quantity":"9687","Price":{"OriginalPrice":"899.00","MarginPrice":"899","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"899","DisplayedMoneys":{"Money":"138.05"}},"ConvertedPrice":"138.05$","ConvertedPriceWithoutSign":"138.05","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"899.00","MarginPrice":"899","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"899","DisplayedMoneys":{"Money":"138.05"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"4209035"}},{"#attributes":{"Pid":"5919063","Vid":"3266779"}}]}},{"Id":"3657280986472","Quantity":"9932","Price":{"OriginalPrice":"919.00","MarginPrice":"919","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"919","DisplayedMoneys":{"Money":"141.12"}},"ConvertedPrice":"141.12$","ConvertedPriceWithoutSign":"141.12","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"919.00","MarginPrice":"919","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"919","DisplayedMoneys":{"Money":"141.12"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"4209035"}},{"#attributes":{"Pid":"5919063","Vid":"3266781"}}]}},{"Id":"3657280986473","Quantity":"9959","Price":{"OriginalPrice":"949.00","MarginPrice":"949","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"949","DisplayedMoneys":{"Money":"145.73"}},"ConvertedPrice":"145.73$","ConvertedPriceWithoutSign":"145.73","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"949.00","MarginPrice":"949","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"949","DisplayedMoneys":{"Money":"145.73"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"4209035"}},{"#attributes":{"Pid":"5919063","Vid":"3266785"}}]}},{"Id":"3657280986474","Quantity":"9965","Price":{"OriginalPrice":"989.00","MarginPrice":"989","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"989","DisplayedMoneys":{"Money":"151.87"}},"ConvertedPrice":"151.87$","ConvertedPriceWithoutSign":"151.87","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"989.00","MarginPrice":"989","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"989","DisplayedMoneys":{"Money":"151.87"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"4209035"}},{"#attributes":{"Pid":"5919063","Vid":"3266786"}}]}},{"Id":"3657280986475","Quantity":"9409","Price":{"OriginalPrice":"949.00","MarginPrice":"949","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"949","DisplayedMoneys":{"Money":"145.73"}},"ConvertedPrice":"145.73$","ConvertedPriceWithoutSign":"145.73","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"949.00","MarginPrice":"949","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"949","DisplayedMoneys":{"Money":"145.73"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"6630567"}},{"#attributes":{"Pid":"5919063","Vid":"6536025"}}]}},{"Id":"3657280986476","Quantity":"9661","Price":{"OriginalPrice":"979.00","MarginPrice":"979","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"979","DisplayedMoneys":{"Money":"150.34"}},"ConvertedPrice":"150.34$","ConvertedPriceWithoutSign":"150.34","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"979.00","MarginPrice":"979","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"979","DisplayedMoneys":{"Money":"150.34"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"6630567"}},{"#attributes":{"Pid":"5919063","Vid":"3266779"}}]}},{"Id":"3657280986477","Quantity":"9911","Price":{"OriginalPrice":"999.00","MarginPrice":"999","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"999","DisplayedMoneys":{"Money":"153.41"}},"ConvertedPrice":"153.41$","ConvertedPriceWithoutSign":"153.41","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"999.00","MarginPrice":"999","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"999","DisplayedMoneys":{"Money":"153.41"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"6630567"}},{"#attributes":{"Pid":"5919063","Vid":"3266781"}}]}},{"Id":"3657280986478","Quantity":"9954","Price":{"OriginalPrice":"1029.00","MarginPrice":"1029","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"1029","DisplayedMoneys":{"Money":"158.02"}},"ConvertedPrice":"158.02$","ConvertedPriceWithoutSign":"158.02","CurrencySign":"$","CurrencyName":"USD","IsDeliverable":"true","DeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"OneItemDeliveryPrice":{"OriginalPrice":"0","MarginPrice":"0","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"0","DisplayedMoneys":{"Money":"0"}}},"PriceWithoutDelivery":{"OriginalPrice":"1029.00","MarginPrice":"1029","OriginalCurrencyCode":"CNY","ConvertedPriceList":{"Internal":"1029","DisplayedMoneys":{"Money":"158.02"}}}},"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"6630567"}},{"#attributes":{"Pid":"5919063","Vid":"3266785"}}]}}]}}');
var configs = [];
configs[10004] = "513661344";
configs[20122] = "103646";
configs[1627207] = "38330499";
configs[5919063] = "6536025";
function updateConfig(pid, vid) {
var id = null;
for(var i = 0; i < details.ConfiguredItems.OtapiConfiguredItem.length; i++) {
var OtapiConfiguredObj = details.ConfiguredItems.OtapiConfiguredItem[i];
var current = [];
for(var j = 0; j < OtapiConfiguredObj.Configurators.ValuedConfigurator.length; j++) {
var ValuedConfiguratorObj = OtapiConfiguredObj.Configurators.ValuedConfigurator[j];
current[ValuedConfiguratorObj['#attributes'].Pid] = ValuedConfiguratorObj['#attributes'].Vid;
}
if(JSON.stringify(current) === JSON.stringify(configs)) {
id = OtapiConfiguredObj.Id;
break;
}
}
console.log(id); // Display result ID
}
updateConfig();
</script>
</head>
</html>
Converting obj2 to a Map and iterating over the ValuedConfigurator using .every() makes it pretty simple.
// Object 1
const obj1 = {"Configurators":{"ValuedConfigurator":[{"#attributes":{"Pid":"1627207","Vid":"38330499"}},{"#attributes":{"Pid":"10004","Vid":"513661344"}},{"#attributes":{"Pid":"20122","Vid":"103646"}},{"#attributes":{"Pid":"5919063","Vid":"6536025"}}]}}
// Object 2 (Pid:Vid,Pid:Vid,Pid:Vid,Pid:Vid)
const obj2 = {"1627207":"38330499","10004":"513661344","20122":"103646","5919063":"6536025"}
const vc = obj1.Configurators.ValuedConfigurator;
const m = new Map(Object.entries(obj2));
const areEqual = vc.length == m.size &&
vc.every(({"#attributes":{Pid, Vid}}) => m.get(Pid) === Vid);
console.log(areEqual);
Related
How do you get Math.max to give you the variables name [closed]
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 1 year ago. Improve this question i've written var i = 0; var x = 0; var y = 0; var z = 0; each variable is incremented by the user, however when i write function blue() { console.log(Math.max(i, x, y, z)) } then I simply get back the highest number, not the name of the highest variable. The goal for this code eventually is to tell the user what number they are, and link them to the corresponding number's information. I've tried converting to an array however I'm a beginner with javascript and unsure where to go from here, would greatly appreciate any advice :) repository for code in question is: https://github.com/LukeMcHenry311/showingproblem inside of quiz folder test.html test.js
var i = 1; var x = 3; var y = 2; var z = 0; console.log(Object.entries({i,x,y,z}).sort(([, a], [, b]) => b - a)[0]);
You can use Array.prototype.reduce() with Object.entries(): var i = 1; var x = 3; var y = 2; var z = 0; max = Object.entries({ i, x, y, z }).reduce(function(carry, [key, value]) { if (value > carry.value) { carry.key = key carry.value = value } return carry }, { 'key': null, 'value': null }); console.log(max); // { key: 'x', value: 3 }
How can we change heading with respect to time using javascript? [closed]
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago. Improve this question I want to change the heading of the web page. Like I have <h1>hey there!</h1> so want to change this heading text with another text with respective time. and changes should be in the loop. like I used my values to be store in an array. eg. var values = [ 'hi', 'hello', 'bye'] how can I use the set.timeout function in for loop to change those values continuously.
You can try like this. let targetElement = document.querySelector('h1'); var values = ['hi', 'hello', 'bye']; var totalArraylength = values.length; var counter = 0; setInterval(() => { if(counter<totalArraylength){ targetElement.innerHTML = values[counter]; counter += 1; }else{ counter = 0 } }, 1000); <h1>hey there!</h1>
Title can be changed by using the title tag. If you wanna count how many times for the loop, you can implement it by taking a look on the above answers. const title = document.querySelector('title') const values = [ 'hi', 'hello', 'bye'] values.forEach((elem) => { setInterval(() => { title.innerText = elem }, 2000) })
In a simple manner with DOM you can do it like this way var count = 0; var titleList = ['hi', 'hello', 'bye']; var intervalsFunc = setInterval(() => { if ( count >= (titleList.length - 1)) { clearInterval(intervalsFunc); } document.getElementsByTagName("h1")[0].innerHTML = titleList[count] ; count++ }, 1000); <!DOCTYPE html> <html> <body> <h1>JavaScript can Change HTML</h1> </body> </html>
let values = ['My', 'array', 'values']; let currentIndex = 0; let changeValue = () => { let node = document.querySelector('h1'); node.innerText = values[currentIndex]; if (currentIndex < values.length + 1) { setTimeout(() => { currentIndex++; changeValue(); }, 1000); } } changeValue() <h1>hey there!</h1>
How to Encode Tags Entities with JavaScript [closed]
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 8 years ago. Improve this question How to encode Tags with .innerHTML array encode html entities Does not work well var find = ['<','>','"']; var replace = ['<','>','"']; window.onload = function() { var code = document.getElementsByTagName("code"); cl = code.length; for (var e = 0; e < cl; e++) code[e].innerHTML = code[e].innerHTML.replace(find,replace); };
You're safer creating a text node. Any needed escapes will be done for you: window.onload = function() { var code = document.getElementsByTagName("code"); cl = code.length; for (var e = 0; e < cl; e++) { var tx = code[e].innerHTML; code[e].innerHTML = ""; var n = document.createTextNode(tx); code[e].appendChild(n); } }; <p><code>Escaped: <b><i>foo</i></b></code></p> <p>Not: <b><i>foo</i></b></p>
How to get attribute of href on the basis of selected text? [closed]
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 8 years ago. Improve this question I have a link like this one <a href="site.com/register.php?mid=username&mode=bn&bid=1"> Now i need to get value of mid or bid on the basis of site.com/register.php in a page. and need to do in javascript. http is also there. How to do that. Thank you.
Try this : put id for anchor tag <a id="anchor1" href="site.com/register.php?mid=username&mode=bn&bid=1"> use below javascript <script> var href = document.getElementById('anchor1').href; //get index of ? var indexStart = href.indexOf('?'); var indexLast = href.length; //get href from ? upto total length var params = href.substring(indexStart+1, indexLast); //get tokens with seperator as '&' and iterate it var paramsArray = params.split("&"); for(var i=0;i<paramsArray.length;i++) { //get key and value pair var paramKeyValue = paramsArray[i]; var keyValue = paramKeyValue.split('='); alert("Key="+keyValue[0]+" and value="+keyValue[1]); } </script> Demo Link
var Links = document.getElementsByTagName('a'); var Values = {}; for(var i = 0; i < Links.length; i++) { if(Links[i].href.toString().indexOf('site.com/register.php') != -1) { var Str = Links[i].href.toString(); var Arr = Str.split('?'); Arr[1] = Arr[1] || ''; var Params = Arr[1].split('&'); for(var i = 0; i < Params.length; i++) { var Value = Params[i].split('='); Values[Value[0]] = Value[1] || null; } break; } } console.log(Values);
convert string to array of numbers in javascript [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself. Closed 8 years ago. Improve this question How to convert below String into arrays in javascript var data = "[ [[1400586475733,-1], [1400586535736,-1], [1400586595739,-1], [1400586655742,-1], [1400586715745,-1]], [[1400586475733,0], [1400586535736,0], [1400586595739,0], [1400586655742,0], [1400586715745,0]] ]"; expected output is data[0] = [[1400586475733,-1],[1400586535736,-1],[1400586595739,-1],[1400586655742,-1],[1400586715745,-1]]; ==> of type object Array data[0][0] = 1400586475733 ==> of type number data[0][1] = -1 ==> of type number
I haven't tried it out thoroughly, but it should work: var string = "[[[1400586475733,-1],[1400586535736,-1],[1400586595739,-1],[1400586655742,-1],[1400586715745,-1]],[[1400586475733,0],[1400586535736,0],[1400586595739,0],[1400586655742,0],[1400586715745,0]]]"; var dataFromString = string.split(","); var finalArray = []; var arrayStartRegEx = new RegExp(/^\[/); var arrayEndRegEx = new RegExp(/\]$/); var currentArray = finalArray; var arrayHistory = []; for(var i = 0; i < dataFromString.length; i++) { var currentString = dataFromString[i]; var closingArray = false; while(arrayStartRegEx.test(currentString) === true) { // Save previous array // createArray // add to big array // currentArray = createdArray // remove bracket var arr = [] arrayHistory.push(currentArray); currentArray.push(arr); currentArray = arr; currentString = currentString.slice(1,currentString.length - 1); } while(arrayEndRegEx.test(currentString) === true){ // remove bracket // add element to array // close currentArray // currentArray = big array currentString = currentString.slice(0,-1); if(!closingArray) { var string = currentString.replace(/\]/g, ''); // Use parseFloat if you know it's going to be a float currentArray.push(parseInt(string)); closingArray = true; } currentArray = arrayHistory.pop(); } if(arrayStartRegEx.test(currentString) === false && arrayEndRegEx.test(currentString) === false) { // add element to array currentArray.push(parseInt(currentString)) } } console.log(finalArray);