JSFiddle - React, Tailwind, and code Playground

by kkdaily

JavaScript

var category = {
	keyword: "ITALIAN",
  locations: [
  	{
    	city: "PROVIDENCE",
      state: "RI"
    },
    {
    	city: "PAWTUCKET",
      state: "RI"
    },
    {
    	city: "JOHNSTON",
      state: "RI"
    },
    {
    	city: "PROVIDENCE",
      state: "RI"
    }
  ]
}


var ugh = category.locations.reduce((acc, item) => {
    const state = item.state;
    if (!acc[state]) acc[state] = [];
    acc[state].push(item.city);
    return acc;
  }, {});
  
  alert(JSON.stringify(ugh))