3

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:

  1. If product 1, output you have 2 One Plus
  2. 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?

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
user3872094
  • 3,269
  • 8
  • 33
  • 71
  • 3
    JSON is always a string. – Andreas Jun 02 '20 at 17:55
  • 2
    Why are you doing `JSON.stringify(obj)` as key instead of just using `obj.Brand` – adiga Jun 02 '20 at 17:56
  • @user4642212 Is there a reason you rolled back that edit? It's quite clear that `string` isn't the keyword here. – Brad Jun 02 '20 at 17:57
  • @Brad I didn’t roll back the edit, I edited at the same time and looked back into the revision history to reapply your changes. It’s faster than to start from scratch. – Sebastian Simon Jun 02 '20 at 17:59
  • 1
    I wouldn't use `JSON` at all. Just group and count: [Group array of objects by value and get count of the groups](https://stackoverflow.com/q/61505921/215552), then reduce the keys and counts. – Heretic Monkey Jun 02 '20 at 18:04
  • here you can make use of load please follow link https://stackoverflow.com/questions/23600897/using-lodash-groupby-how-to-add-your-own-keys-for-grouped-output – Rakesh Jun 02 '20 at 18:18

5 Answers5

5

How about this?

var product1 = [{
    "Brand": "One Plus"
  },
  {
    "Brand": "One Plus"
  }
];
var product2 = [{
    "Brand": "One Plus"
  },
  {
    "Brand": "Apple"
  }
];

function countProducts(arr) {
  let counter = arr.reduce((acc, val) =>
    (acc[val.Brand] = (acc[val.Brand] || 0) + 1, acc), {});
  let strings = Object.keys(counter).map(k => `${counter[k]} ${k}`);
  return `You have ${strings.join(' and ')}`;
}

console.log(countProducts(product1));

console.log(countProducts(product2));
gyohza
  • 736
  • 5
  • 18
2
const product1s = product1.reduce((acc, product) => {
acc[product.Brand] = (acc[product.Brand] || 0) + 1;
return acc;
}, {});

console.log(
`You have ${
Object.keys(product1s).map(product => `${product1s[product]} ${product}`).join(" and ")
}`
);
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Ibraheem
  • 2,168
  • 20
  • 27
2

const product1 = [
  {Brand: "One Plus"},
  {Brand: "One Plus"},
];
const product2 = [
  {Brand: "One Plus"},
  {Brand: "Apple"},
];

function whatYouHaveIn(list) {
  return `You have ` + Object.entries(list.reduce((a, c) => {
    a[c.Brand] = a[c.Brand] || 0;
    a[c.Brand]++;
    return a;
  }, {})).map(([brand, count]) => `${count} ${brand}`).join(` and `);
}

console.log(whatYouHaveIn(product1));

console.log(whatYouHaveIn(product2));
Chase
  • 3,028
  • 14
  • 15
0

You first have to count the occurances, than its just a matter of string concat or templates

function strinify(a) {
  let occurances = {};
  
  a.forEach(function(e,i){
   if( e.Brand in occurances ) {
    occurances[e.Brand] ++;
   } else {
    occurances[e.Brand] = 1;
   }
  });
   
  let str = [];
  
  for( brand in occurances ){
    count = occurances[brand];
    str.push(count + " " + brand);
  }; 
  
  return "you have " + str.join(" and ");
}
  
console.log(strinify([
    {
      "Brand": "One Plus"
    },
    {
      "Brand": "One Plus"
    },
    {
      "Brand": "Apple"
    }
]));
on8tom
  • 1,766
  • 11
  • 24
0

const order1 = [
  {
    "Brand": "One Plus",
    "test" : "test"
  },
  {
    "Brand": "One Plus"
  },
  {
   "Bar": "Foo"
  }
];
const order2 = [
  {
    "Brand": "One Plus"
  },
  {
    "Brand": "Apple"
  }
];

const orderToString = (order) => {
  if(order == null) return;
  
  const productsWithBrand = order.filter(product => Object.keys(product).includes('Brand'));
  const productBrandCounter = (counter, product) => (counter[product.Brand] = (counter[product.Brand] || 0) + 1, counter);
  const countPerProduct = productsWithBrand.reduce(productBrandCounter, {});
  const stringPerProduct = Object.keys(countPerProduct).map(brand => `${countPerProduct[brand]} ${brand}`);

  return `You have ${stringPerProduct.join(' and ')}`;
}

console.log(orderToString(order1));
Jens Ingels
  • 806
  • 1
  • 9
  • 12