Untitled
unknown
plain_text
2 years ago
805 B
4
Indexable
var cityData = [ { state: "State 1", city: "City 1", pinCode: "123456" }, { state: "State 2", city: "City 2", pinCode: "789012" }, { state: "State 1", city: "City 3", pinCode: "345678" }, { state: "State 2", city: "City 4", pinCode: "901234" } ]; var stateData = {}; // Loop through the cityData array for (var i = 0; i < cityData.length; i++) { var cityObj = cityData[i]; var state = cityObj.state; var city = cityObj.city; var pinCode = cityObj.pinCode; // Check if the state exists in the stateData object if (!stateData[state]) { // If the state does not exist, create a new object for it stateData[state] = { data: [] }; } // Push the city object to the state's data array stateData[state].data.push({ city: city, pinCode: pinCode }); } console.log(stateData);
Editor is loading...