Counting elements in JSON using JavaScript
Counting elements in JSON using JavaScript
I hope someone can point me in the right direction, I've burnt hours trying to solve what I think is a simple problem - I've hunted SO and tried various things that were close but no cigar.
I want to count two things from the sample JSON block below.
First I want to count the number of productLine entries, and second I want to count the number of types - by productLine and in total across all productLine(s).
So I'd like to end up with 3 variables with a number assigned to them to represent these three values.
Is anyone able to help?
var products = { "product": "Product Name", "productLines": [{ "productLine": "Line 1", "types": ["Type 1", "Type 2", "Type 3", "Type 4"] }, { "productLine": "Line 2", "types": ["Type 5", "Type 6", "Type 7", "Type 8"] }, { "productLine": "Line 3", "types": ["Type 9", "Type 10", "Type 11"] }, { "productLine": "Line 4", "types": ["Type 12", "Type 13"] }] };
Output would be something like:
var productLineCount = 4 var productLine[0].name = "Line 1" var productLine[0].types.count() = 4 var typesCount = 13
Answer by Rajaprabhu Aravindasamy for Counting elements in JSON using JavaScript
You can do it by,
var prodLines = products.productLines.length; var prodLinesTypes = products.productLines.reduce(function(a,b){ return a + (b.types.length) }, 0); console.log(prodLines); //4 console.log(prodLinesTypes); //13
The first one is an obvious one, that is reading the length of an array. But the second one can be done by using .reduce()
. It can be done in many ways. But I would suggest you to go with reduce at this context.
Answer by Nenad Vracar for Counting elements in JSON using JavaScript
You can use forEach
loop and return results in one object
var products = { "product": "Product Name", "productLines": [{ "productLine": "Line 1", "types": ["Type 1", "Type 2", "Type 3", "Type 4"] }, { "productLine": "Line 2", "types": ["Type 5", "Type 6", "Type 7", "Type 8"] }, { "productLine": "Line 3", "types": ["Type 9", "Type 10", "Type 11"] }, { "productLine": "Line 4", "types": ["Type 12", "Type 13"] }] }; var result = {} products.productLines.forEach(function(e) { result.totalLines = (result.totalLines || 0) + 1; e.types.forEach(function(a) { result.totalTypes = (result.totalTypes || 0) + 1; result[e.productLine + 'Types'] = (result[e.productLine + 'Types'] || 0) + 1; }); }); document.body.innerHTML = '' + JSON.stringify(result, 0, 4) + '
';
Answer by Ayan for Counting elements in JSON using JavaScript
You can use an custom api like
getter(index, type)
index is the product index, type can be either typeCount
or name
.
var products = { "product": "Product Name", "productLines": [{ "productLine": "Line 1", "types": ["Type 1", "Type 2", "Type 3", "Type 4"] }, { "productLine": "Line 2", "types": ["Type 5", "Type 6", "Type 7", "Type 8"] }, { "productLine": "Line 3", "types": ["Type 9", "Type 10", "Type 11"] }, { "productLine": "Line 4", "types": ["Type 12", "Type 13"] }] }; var count = 0; for (var i = 0, len = products.productLines.length; i < len; i += 1) { count += products.productLines[i].types.length; } console.log('productLineCount: ',len); console.log('typesCount: ', count); function getter(index, type) { var res, arr = products.productLines[index]; if (type === 'name') { res = arr.productLine; } else if (type === 'typeCount') { res = arr.types.length; } return res; } console.log('productLine[0].name: ', getter(0, 'name')); console.log('productLine[0].types.count(): ', getter(0, 'typeCount')); /*console.log('productLine[1].name: ', getter(1, 'name')); console.log('productLine[1].types.count(): ', getter(1, 'typeCount')); console.log('productLine[2].name: ', getter(2, 'name')); console.log('productLine[2].types.count(): ', getter(2, 'typeCount'));*/
Answer by trincot for Counting elements in JSON using JavaScript
You could get the three counts as follows:
var productLineCount = products.productLines.length; var productTypesCount = products.productLines.map(line => line.types.length); var totalTypesCount = productTypesCount.reduce((sum, cur) => sum + cur, 0);
var products = { "product": "Product Name", "productLines": [{ "productLine": "Line 1", "types": ["Type 1", "Type 2", "Type 3", "Type 4"] }, { "productLine": "Line 2", "types": ["Type 5", "Type 6", "Type 7", "Type 8"] }, { "productLine": "Line 3", "types": ["Type 9", "Type 10", "Type 11"] }, { "productLine": "Line 4", "types": ["Type 12", "Type 13"] }] }; var productLineCount = products.productLines.length; var productTypesCount = products.productLines.map(line => line.types.length); var totalTypesCount = productTypesCount.reduce((sum, cur) => sum + cur, 0); console.log('productLineCount', productLineCount); console.log('productTypesCount', productTypesCount); console.log('totalTypesCount', totalTypesCount);
Answer by Joab Mendes for Counting elements in JSON using JavaScript
For counting the ProductLines you can do it
number_of_productLines = products.productLines.length
And for counting the types by productLines you can do:
productlines_types_count = [] # List of {'productline': name, 'length': int} products.productLines.forEach(function(productline){ productlines_types_count.push({'productline': productline.productLine, 'length': productline.types.length}) });
Also fix your json, the last of objects has a 'type' id insteade of 'types':
var products = { "product" : "Product Name", "productLines": [ { "productLine" : "Line 1", "types": ["Type 1", "Type 2", "Type 3", "Type 4"] }, { "productLine": "Line 2", "types": ["Type 5", "Type 6", "Type 7", "Type 8"] }, { "productLine": "Line 3", "types": ["Type 9", "Type 10", "Type 11"] }, { "productLine": "Line 4", "types": ["Type 12", "Type 13"] #here }] };
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment