How can we change heading with respect to time using javascript? [closed] - javascript

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>

Related

simple javascript number game [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 5 months ago.
Improve this question
substract 7 every time (starting number is 100), the first answer will be 93, i dont want user to move on until he gets the first answer right
let answerInput = document.getElementById("inpu");
let checkButton = document.getElementById("send");
let answers = [];
for(let i = 100; i>2;){
i -= 7;
answers.push(i);// [93, 86,...]
}
checkButton.onclick = function(){
//my struggle here
if(isNaN(answerInput.value) === true){
answerInput.value = "";
answerInput.placeholder = "only numbers are allowed";
}
}
There is no need to store the numbers in an array, seen as the next number is always 7 from the last, just keep a counter for the number. And then just compare this to the input - 7;
eg.
const span = document.querySelector('span');
const input = document.querySelector('input');
const button = document.querySelector('button');
let number = 100;
const showNum = () => span.innerText = number;
showNum();
button.addEventListener('click', () => {
const want = number - 7;
if (Number(input.value) === want) {
if (want == 2) {
alert("Well done you..");
} else {
number -= 7;
showNum();
}
}
});
Subtract 7 from <span></span> = <input type="number" value="100"/><button>Try</button>
Solution:
let answerInput = document.getElementById("inpu");
let checkButton = document.getElementById("send");
let answers = [];
for (let i = 100; i > 2; ) {
i -= 7;
answers.push(i); // [93, 86,...]
}
checkButton.onclick = function () {
if (isNaN(answerInput.value) === true) {
answer.value = "";
answer.placeholder = "only numbers are allowed";
}
// check if answer correct, `!=` compare with type coercion
else if (answerInput.value != answers[0]) {
answer.placeholder = "try again"
}
else {
answers.shift(); // remove first array element
answer.placeholder = "correct!"
}
};
<!DOCTYPE html>
<html>
<head></head>
<body>
<!--
You can use type="number" to auto-avoid letters in field:
<input id=inpu type="number" />
-->
<input id=inpu />
<input disabled id=answer />
<button id=send>Send</button>
</body>
</html>

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 }

I’m trying to write a function in javascript that Returns the number of times a number occurs as a digit in another number [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 2 years ago.
Improve this question
The second input number will always be between 0 and 9, and I must use the filter method. This is what I have so far:
const countOccurrencesFilter = (number, target) => {
let numStrArr = number.toString().split('');
let targetStr = target.toString();
let count = numStrArr.filter = (numStrArr => numStrArr[0] === targetStr).length;
return count;
};
You're almost there, you're assigning a key to numStrArr named filter whereas in actual you need to call the method. you need use
numStrArr.filter(numStrArr ...
not
numStrArr.filter = (numStrArr
const countOccurrencesFilter = (number, target) => {
let numStrArr = number.toString().split('');
let targetStr = target.toString();
let count = numStrArr.filter(numStrArr => numStrArr === targetStr).length;
return count;
};
console.log(countOccurrencesFilter(121, 1))
console.log(countOccurrencesFilter(000, 1))
P.S:- Also no sense of using numStrArr[0] as you splited values will always be one character long only as you're spliting with ''
I think this will solve your purpose.
const countOccurrencesFilter = (number, target) => {
let numStrArr = number.toString().split('');
let targetStr = target.toString();
let count = numStrArr.filter(digit => digit === targetStr).length;
return count;
};
console.log(countOccurrencesFilter(11211, 1));
Try changing
let count = numStrArr.filter = (numStrArr => numStrArr[0] === targetStr).length;
To
let count = numStrArr.filter(numStrArr => numStrArr === targetStr).length;

Running Loop showing results [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 3 years ago.
Improve this question
How can I use a loop to flip a coin 20 times, each time displaying the result of the flip as a string on the page? After the loop completes, I want to return the array with the result of each flip.
What I have so far:
function display20Flips() {
const results = [];
var i;
var results = '';
for (i = 0; i < 20; i++) {
}
const coin = {
state: 0,
flip: function() {
this.state = Math.floor(Math.random() * 2);
},
toString: function() {
if (this.state === 0) {
return Heads;
} else {
return Tails
}
},
toHTML: function() {
const image = document.createElement('img');
image.src = `$this.toString()}.png`
image.alt = this.toString()
return image;
}
};
function display20Flips() {
const results = [];
// 4. One point: Use a loop to flip the coin 20 times, each time displaying the result of the flip as a string on the page. After your loop completes, return an array with the result of each flip.
for (let i = 0; i < 20; i++) {
if(i%2){
results[i] = 'heads';
} else{
results[i] = 'tails';
}
console.log(results[i])
}
}
display20Flips();
function display20Images() {
const results = [];
// 5. One point: Use a loop to flip the coin 20 times, and display the results of each flip as an image on the page. After your loop completes, return an array with result of each flip.
}
You might want to create a FlipCoin function which determinates the value, call this function in a loop, storing the function result value in a variable, which you can print to screen and add to a results array.
something like this
// Returns 1 or 0, i.e. 1 = Head, 0 = Tail
function flipCoin() {
var flipValue = Math.floor(Math.random() * 100);
return flipValue % 2;
}
function main() {
let results = [];
for (cnt = 0; cnt < 20; cnt++) {
let result = flipCoin();
results.push(result);
console.log(result == 0 ? "Tail" : "Head");
}
console.dir(results);
}
main();
I believe you want something like this:
function display20Flips() {
var results = [];
for (let i = 0; i < 20; i++) {
if(i%2){
results[i] = 'heads';
} else{
results[i] = 'tails';
}
console.log(results[i])
}
}
display20Flips(); // for displaying in snippet code
Use Array(20) to create an array of size 20
Array.map to iterate through each element, use Array.fill to fill the array with null values else it will not iterate through.
Math.random()*10 to generate a random number between 0-10.
If the value is even then returned Head else Tails
function display20Flips() {
return Array(20).fill().map(e => Math.random()*10 & 1 ? 'Head' : 'Tails')
}
console.log(display20Flips())
Im not an expert but here it is :D
<!DOCTYPE html>
<html>
<body>
<script>
var results = [];
function display20Flips() {
for (var i = 0; i <20; i++ ){
var x = Math.floor((Math.random() * 2));
if(x == 0){
results[i] = "heads";
}else{
results[i] = "tails";
}
}
console.log(results);
}
display20Flips();
</script>
</body>
</html>

Check if two objects with different structure are identical? [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 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);

Categories

Resources