Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
836 B
2
Indexable
Never
var cityData = [
    {
        state: "California",
        city: "Los Angeles",
        pincode: "90001"
    },
    {
        state: "California",
        city: "San Francisco",
        pincode: "94102"
    },
    {
        state: "New York",
        city: "New York City",
        pincode: "10001"
    },
    {
        state: "Texas",
        city: "Houston",
        pincode: "77001"
    },
    {
        state: "Texas",
        city: "Austin",
        pincode: "73301"
    }
];

var cityDict = {};

for (var i = 0; i < cityData.length; i++) {
    var city = cityData[i];
    var state = city.state;
    var cityObj = {
        city: city.city,
        pincode: city.pincode
    };

    if (state in cityDict) {
        cityDict[state].push(cityObj);
    } else {
        cityDict[state] = [cityObj];
    }
}

console.log(cityDict);