Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Bugs & Fixes 2 years ago
Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
This is a smaller version of the array i have but it has the same structure
with const arr below, i want to create 2 new arrays with unique values that are sorted in ascending order
const arr = [{ tags: ['f', 'b', 'd'], weight: 7, something: 'sdfsdf' }, { tags: ['a', 'b', 'c', 'd', 'e'], weight: 6, something: 'frddd' }, { tags: ['f', 'c', 'e', 'a'], weight: 7, something: 'ththh' }, { tags: ['a', 'c', 'g', 'e'], weight: 5, something: 'ghjghj' } ]; const finalTags = []; const finalWeight = []; // TODO: find a better way to do this arr.forEach(v => { if (finalWeight.indexOf(v.weight) === -1) finalWeight.push(v.weight); v.tags.forEach(val => { if (finalTags.indexOf(val) === -1) finalTags.push(val);}); }); // Ascending order finalTags.sort(); finalWeight.sort();what i have above works, but seems a bit messy and was wandering if there was a better/tidier way of doing this
const arr = [{ tags: ['f', 'b', 'd'], weight: 7, something: 'sdfsdf' }, { tags: ['a', 'b', 'c', 'd', 'e'], weight: 6, something: 'frddd' }, { tags: ['f', 'c', 'e', 'a'], weight: 7, something: 'ththh' }, { tags: ['a', 'c', 'g', 'e'], weight: 5, something: 'ghjghj' } ]; const finalTags = []; const finalWeight = []; // TODO: find a better way to do this arr.forEach(v => { if (finalWeight.indexOf(v.weight) === -1) finalWeight.push(v.weight); v.tags.forEach(val => { if (finalTags.indexOf(val) === -1) finalTags.push(val);
}); }); // Ascending order finalTags.sort(); finalWeight.sort();
what i have above works, but seems a bit messy and was wandering if there was a better/tidier way of doing this
One solution is to use Array.reduce() to create two sets, one with the tags and other with the weights. After this you can transform the sets to arrays and use Array.sort() on they:
tags
weights
sets
arrays
const arr = [ { tags: ['f', 'b', 'd'], weight: 7, something: 'sdfsdf' }, { tags: ['a', 'b', 'c', 'd', 'e'], weight: 6, something: 'frddd' }, { tags: ['f', 'c', 'e', 'a'], weight: 7, something: 'ththh' }, { tags: ['a', 'c', 'g', 'e'], weight: 5, something: 'ghjghj' } ]; let res = arr.reduce((acc, {tags, weight}) => { acc.tags = new Set([...acc.tags, ...tags]); acc.weights.add(weight); return acc; }, {tags: new Set(), weights: new Set()}); let sortedWeigths = [...res.weights].sort(); let sortedTags = [...res.tags].sort((a, b) => a.localeCompare(b)); console.log("weights: ", sortedWeigths, "tags: ", sortedTags);.as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;}
const arr = [ { tags: ['f', 'b', 'd'], weight: 7, something: 'sdfsdf' }, { tags: ['a', 'b', 'c', 'd', 'e'], weight: 6, something: 'frddd' }, { tags: ['f', 'c', 'e', 'a'], weight: 7, something: 'ththh' }, { tags: ['a', 'c', 'g', 'e'], weight: 5, something: 'ghjghj' } ]; let res = arr.reduce((acc, {tags, weight}) => { acc.tags = new Set([...acc.tags, ...tags]); acc.weights.add(weight); return acc; }, {tags: new Set(), weights: new Set()}); let sortedWeigths = [...res.weights].sort(); let sortedTags = [...res.tags].sort((a, b) => a.localeCompare(b)); console.log("weights: ", sortedWeigths, "tags: ", sortedTags);
.as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;}
You could use the following code. This basically splits arr up into finalTags and finalWeights.
arr
finalTags
finalWeights
.flat() flattens the array ([1, [2, [3]]] would become [1, 2, 3])
[1, [2, [3]]]
[1, 2, 3]
And finally, [...new Set(finalTags)].sort() gets rid of duplicates and sorts the array.
[...new Set(finalTags)].sort()
const arr = [{ tags: ['f', 'b', 'd'], weight: 7, something: 'sdfsdf' }, { tags: ['a', 'b', 'c', 'd', 'e'], weight: 6, something: 'frddd' }, { tags: ['f', 'c', 'e', 'a'], weight: 7, something: 'ththh' }, { tags: ['a', 'c', 'g', 'e'], weight: f="https://forum.tuteehub.com/tag/5">5, something: 'ghjghj' } ]; let finalTags = arr.map(e => e.tags); finalTags = finalTags.flat(); finalTags = [...new Set(finalTags)].sort(); let finalWeight = arr.map(e => e.weight); finalWeight = [...new Set(finalWeight)].sort(); console.log(finalTags); console.log(finalWeight);
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.