In: Computer Science
JavaScript
each item must have at least three categorical attributes and at least one numeric attribute.
Write a function that counts the number of values each item attribute has. This is effectively a group-by on the item table for each column. For example: if the first attribute is colour, and its values are [ ‘red’, ‘black’, ‘white’, ‘black’, ‘red’ ],
the counts for colour would be: red 2 black 2 white 1

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript GroupBy</h2>
<script>
var items = {color: ['red', 'red', 'white', 'black', 'white'], style: ['long', 'long', 'short', 'short', 'medium', 'long'], size: [6, 8, 10, 12, 6, 12, 10]}
function groupBy(items) {
var result = {}
for (var prop in items) {
var r = {}
var arr = items[prop]
for(var i=0; i<arr.length; i++) {
if(arr[i] in r) {
r[arr[i]] += 1;
} else {
r[arr[i]] = 1;
}
}
result[prop] = r
}
return result
}
console.log(groupBy(items))
</script>
</body>
</html>
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.
function groupBy(arr) { var result = { for(var i=0; i<arr.length; i++) { if(colors[i] in result) { result[colors[i]] += 1; } else { result[colors[i]] = 1; return result console.log(groupBy (colors) </scrint> o Elements Console Sources Network Performanc Do top - url:https://distantsupportives {red: 2, black: 2, white: 1}