I have two arrays as below:
var product1 = [
{
"Brand": "One Plus"
},
{
"Brand": "One Plus"
}
];
var product2 = [
{
"Brand": "One Plus"
},
{
"Brand": "Apple"
}
];
I want to loop through the array and print the following:
- If product 1, output
you have 2 One Plus
- If product 2, output
you have 1 One Plus and 1 Apple
Below is the code that I tried.
var product1 = [
{
"Brand": "One Plus"
},
{
"Brand": "One Plus"
}
];
var product2 = [
{
"Brand": "One Plus"
},
{
"Brand": "Apple"
}
];
counter1 = {}
product1.forEach(function(obj) {
var key = JSON.stringify(obj)
counter1[key] = (counter1[key] || 0) + 1
});
console.log(counter1);
counter2 = {}
product2.forEach(function(obj) {
var key = JSON.stringify(obj)
counter2[key] = (counter2[key] || 0) + 1
});
console.log(counter2);
I’m able to get the JSON output, but how can I get it in the sentence format?