html - Javascript cannot map object with repetative values - Stack Overflow
I need to style specific elements by id using given object.
Keys and values in projectSkillsMap object corresponds to ids in html file. When user clicks on element with id listed in object keys function chooseProject() serves to style elements with ids listed in array of value corresponding to key.
Initial goal to remove class button_click from all of the buttons and add only to listed in the object.
const projectSkillsMap = {
'project1': ['skill_2', 'skill4', 'skill5'],
'project2': ['skill1', 'skill_2', 'skill4', 'skill5', 'skill6'],
'project3': ['skill1', 'skill_2', 'skill3', 'skill4', 'skill5', 'skill6']
}
function chooseProject(key) {
// adding accet to chosen group
Object.keys(projectSkillsMap).map((i) => {
if (i == key) {
Object.entries(projectSkillsMap).map((il, value) => {
if (il == i) {
document.getElementById(value).classList.toggle('button_click')
}
})
}
})
// removing accents from other groups
Object.keys(projectSkillsMap).map((i) => {
if (i !== key) {
projectSkillsMap[`${i}`].map((el) => document.getElementById(`${el}`).classList.remove('button_click'))
}
})
Object.values(projectSkillsMap).map((el) => {
if (el !== key) {
document.getElementById(el).classList.remove('button_click')
}
})
}
When user clicks on a button the chooseProject()
function is triggered with one of the object keys. The problem is that document.getElementById(value).classList.toggle('button_click')
does not work because of repetitive values among keys in my object.
I realized that when I changed my object and left only unique values for each key then the code work as expected.
Is there any way I can make it work?
I need to style specific elements by id using given object.
Keys and values in projectSkillsMap object corresponds to ids in html file. When user clicks on element with id listed in object keys function chooseProject() serves to style elements with ids listed in array of value corresponding to key.
Initial goal to remove class button_click from all of the buttons and add only to listed in the object.
const projectSkillsMap = {
'project1': ['skill_2', 'skill4', 'skill5'],
'project2': ['skill1', 'skill_2', 'skill4', 'skill5', 'skill6'],
'project3': ['skill1', 'skill_2', 'skill3', 'skill4', 'skill5', 'skill6']
}
function chooseProject(key) {
// adding accet to chosen group
Object.keys(projectSkillsMap).map((i) => {
if (i == key) {
Object.entries(projectSkillsMap).map((il, value) => {
if (il == i) {
document.getElementById(value).classList.toggle('button_click')
}
})
}
})
// removing accents from other groups
Object.keys(projectSkillsMap).map((i) => {
if (i !== key) {
projectSkillsMap[`${i}`].map((el) => document.getElementById(`${el}`).classList.remove('button_click'))
}
})
Object.values(projectSkillsMap).map((el) => {
if (el !== key) {
document.getElementById(el).classList.remove('button_click')
}
})
}
When user clicks on a button the chooseProject()
function is triggered with one of the object keys. The problem is that document.getElementById(value).classList.toggle('button_click')
does not work because of repetitive values among keys in my object.
I realized that when I changed my object and left only unique values for each key then the code work as expected.
Is there any way I can make it work?
Share Improve this question edited yesterday NanoMuffin asked yesterday NanoMuffinNanoMuffin 757 bronze badges 12 | Show 7 more comments1 Answer
Reset to default 1Solution provided by Guy Incognito in comments. Look at the fiddle: jsfiddle.net/0k3zLjxb
To sum up: initial goal was to remove the class from all the buttons and then add it to the ones in the object. There was no need to iterate over object keys or values via object methods, simple forEach() would do the trick.
function chooseProject(key) {
document.querySelectorAll(".skill_button").forEach(button => button.classList.remove('button_click'));
projectSkillsMap[key].forEach(value => document.getElementById(value).classList.add('button_click'));
}
- 在沉默中炸裂爆发!2013安卓系统大事记
- [连载]巨头“心血之作”终失败(四):intel Larrabee图形芯片
- python - How to solve this TensorflowTensorboard compatibility problem? - Stack Overflow
- excel - When loading my userform i get Run-Time Error 381: Could not set the list property. Invalid property array index - Stack
- python - Trouble implementing Hamitonian with QutiP - Stack Overflow
- Prisma create: Typescript error in field data - Stack Overflow
- javascript - Alphabetize options in a select list, retain event listeners - Stack Overflow
- C equivalent of C++ inline - Stack Overflow
- tensorflow - Error when loading old .h5 file with latest Keras - Stack Overflow
- swift - SwiftUI ScrollView not scrolling with DragGesture inside a ForEach Item - Stack Overflow
- deno - how to hide freshjspreact source code from Dev Tools - Stack Overflow
- node.js - node-gyp fails with parsing vs version: undefined - Stack Overflow
- zip - Download and unzip file from URL with Github Action - Stack Overflow
- amazon web services - Python boto3: download files from s3 to local only if there are differences between s3 files and local one
- google cloud platform - Querying public google_ads_transparency_center BQ table doesn't show all the needed data - Stack
- eclipse - Floodlight debug Exception in thread "main" java.lang.Error - Stack Overflow
- GHC unable to find mingwinclude directory on Windows - Stack Overflow
projectSkillsMap[key].forEach(value => document.getElementById(value).classList.toggle('button_click'))
would be close. – Guy Incognito Commented yesterdayvalue
in the map will be an integer index of the current iteration. It will not be anyid
of an element in the DOM – Rory McCrossan Commented yesterday