How can i create array of values from dataset? I'm looking for answer everywhere and i can't find anything.
I have 6 img elements i HTML with dataset (there are informations about what is at picture), i've created variable with dataset of all img elements like:
for (let i = 0; i < allImages.length; i++) {
let allDataset1 = [];
allDataset1.push(allImages[i].dataset);
console.log(allDataset1);
Console shows me DOMStringMap with values of dataset and now i need to create an array of this values, or maybe i don't need.. I have to compare user input text to each value of dataset.
You can use the find method on the allDataset1 array to compare the user input text value and check whether the user input matches any values in the allDataset1 array.
The find() method on the array returns the value of the first element in the provided array that satisfies the provided testing function
Assuming the userInput variable has the userInput value stored in it,all you have to do is the following.
const datasetEnteredByUser = allDataset1.find((val) => val === userInput).
datasetEnteredByUser will be undefined if no value in the allDataset1 matches the userInput.
Based upon the discussion in the comments below, allDataset1 is an array of img elements and you have a comma seperated string in the dataset attribute for every img element.
So, i think you would need to create the allDataset1 in the following manner before comparing it with the user input.
let allDataset1 = []
[...allImages].forEach(img => { // Converting the allImages array of Dom elements into a Javascript array to iterate on.
allDataset1 = allDataset1.concat(img.getAttribute('dataset').split(','))
})
You can then the find method on allDataset1 to compare every element in the string with the user input.