Use vanilla JavaScript and ES6 Class dynamic build multi-layers sidebar navigation, eg.…
- User
Use vanilla JavaScript and ES6 Class dynamic build multi-layers sidebar navigation, eg. three-layers,four-layers etc.
- Assistant
- User
class active is only one
- Assistant
If you want only one active class at a time, you can modify the event listener in the
Sidebarclass to remove other active classes before applying it to the clicked element. Here's the updated version of theSidebarclass:class Sidebar { constructor(data, containerId) { this.data = data; this.container = document.getElementById(containerId); this.buildNavigation(this.data, this.container); } buildNavigation(data, parentElement) { const ul = document.createElement('ul'); data.forEach(item => { const li = document.createElement('li'); li.classList.add('nav-item'); const a = document.createElement('a'); a.href = item.url || '#'; a.textContent = item.title; li.addEventListener('click', event => { event.stopPropagation(); if (item.children && item.children.length) { // Remove active class from other items parentElement.querySelectorAll('.nav-item.active').forEach(elem => { if (elem !== li) { elem.classList.remove('active'); const subNav = elem.querySelector('ul'); if (subNav) { subNav.style.display = 'none'; } } }); // Toggle active class for the clicked item li.classList.toggle('active'); const subNav = li.querySelector('ul'); if (subNav) { subNav.style.display = li.classList.contains('active') ? 'block' : 'none'; } } else if (typeof this.onNavigationItemClick === 'function') { this.onNavigationItemClick(event, item); } }); li.appendChild(a); if (item.children && item.children.length) { this.buildNavigation(item.children, li); } ul.appendChild(li); }); parentElement.appendChild(ul); } } - User
no , I want only one item active and click item other auto remove class active
- Assistant
I misunderstood you previously. If you want only one item to be active, including from other layers, you can modify the event listener in the
Sidebarclass by iterating through the.nav-itemelements in the entire sidebar instead of the current layer. Here's the updated version of theSidebarclass:class Sidebar { constructor(data, containerId) { this.data = data; this.container = document.getElementById(containerId); this.buildNavigation(this.data, this.container); } buildNavigation(data, parentElement) { const ul = document.createElement('ul'); data.forEach(item => { const li = document.createElement('li'); li.classList.add('nav-item'); const a = document.createElement('a'); a.href = item.url || '#'; a.textContent = item.title; li.addEventListener('click', event => { event.stopPropagation(); // Remove active class from other items in the entire sidebar this.container.querySelectorAll('.nav-item.active').forEach(elem => { if (elem !== li) { elem.classList.remove('active'); const subNav = elem.querySelector('ul'); if (subNav) { subNav.style.display = 'none'; } } }); // Toggle active class for the clicked item li.classList.toggle('active'); const subNav = li.querySelector('ul'); if (subNav) { subNav.style.display = li.classList.contains('active') ? 'block' : 'none'; } else if (typeof this.onNavigationItemClick === 'function') { this.onNavigationItemClick(event, item); } }); li.appendChild(a); if (item.children && item.children.length) { this.buildNavigation(item.children, li); } ul.appendChild(li); }); parentElement.appendChild(ul); } } - User
for (const [key,value] of Object.entries(obe)) vs Object.entries(obj).forEach(([key,value])) Performance
- Assistant
Both methods of iterating through the entries of an object have similar performance characteristics. However, the difference in performance mainly depends on the JavaScript engine used and the specific scenario, such as the number of keys in the object.
In most cases, the performance difference between these two methods is negligible, and you should choose the one that you find more readable and convenient for your use case.
- Using
for..ofwithObject.entries():
for (const [key, value] of Object.entries(obj)) { // Do something with key and value }- Using
forEach()withObject.entries():
Object.entries(obj).forEach(([key, value]) => { // Do something with key and value });If performance is a significant concern for your specific case, it's essential to benchmark both methods using your specific data and running it on the target platform to see which one is faster.
- Using
- User
const numSome = { front: 3, rear: 1, frontLv: 5, sus: 7 }; const numSomePrice = { front: 300, rear: 100, frontLv: 500, sus: 700 };
I want result: [900,100,2500,4900].reduce((a,b)=>a+b,0); - Assistant
You can achieve this by iterating over the keys of
numSomeand multiplying its values by the corresponding values innumSomePrice. Here's the code to get the desired result:const numSome = { front: 3, rear: 1, frontLv: 5, sus: 7 }; const numSomePrice = { front: 300, rear: 100, frontLv: 500, sus: 700 }; const result = Object.keys(numSome).map(key => { return numSome[key] * numSomePrice[key]; }).reduce((a, b) => a + b, 0); console.log(result); // Output: 8400This code first creates an array of total prices for each part by multiplying the quantity and price using
map(), and then sums up the array usingreduce()to get the total price. - User
const data = { pinPrice: 45, fsCost: 1, jxPrice: 1, dzPrice: 45 }; const test = { 001: { pinPrice: { "[1,4]": 56800, "=5": 60800, "[6": 2000 }, jxPrice: { "[1,4]": 3000, "=5": 3500, "[6": 10000 }, dzPrice: { "[1,20]": 10000, "[21,40]": 20000, "[60": 100000 }, fsCost: 14000, }, };
I want result:{ pinPrice:60800+(45-5)*2000, jxPrice:{"[6": 10000}, dzPrice:{"[21,40]": 20000}, fsCost:1*14000 } - Assistant
I understand that you want to calculate the result based on the
dataobject and the matching values from thetest[001]object. Here's the code to achieve the desired result:const data = { pinPrice: 45, fsCost: 1, jxPrice: 1, dzPrice: 45 }; const test = { 001: { pinPrice: { "[1,4]": 56800, "=5": 60800, "[6": 2000 }, jxPrice: { "[1,4]": 3000, "=5": 3500, "[6": 10000 }, dzPrice: { "[1,20]": 10000, "[21,40]": 20000, "[60": 100000 }, fsCost: 14000, }, }; const calculateValue = (key, value) => { const ranges = test[001][key]; for (const range in ranges) { const [low, high] = range .replace("[", "") .replace("]", "") .split(","); if (!high) { // Check if value is equal to low if (parseInt(low) === value) { return ranges[range]; } } else if (!isNaN(parseFloat(high))) { // Check if value is within the range (low, high) if (value >= parseInt(low) && value <= parseInt(high)) { return ranges[range]; } } else { // Check if value is greater than or equal to low if (value >= parseInt(low)) { return `${value - parseInt(low) + 1} * ${ranges[range]}`; } } } return "Not found"; }; const result = {}; for (const key in data) { if (test[001].hasOwnProperty(key)) { result[key] = calculateValue(key, data[key]); } } console.log(result);
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.