-1

Please help me merge array's cities whereas the state is same and remove duplicate index. The expecting output is given below

Input array

[
    {
        "state": "delhi",
        "cities": "central delhi"
    },
    {
        "state": "jharkhand",
        "cities": "dumka"
    },
    {
        "state": "jharkhand",
        "cities": "deoghar"
    },
    {
        "state": "jharkhand",
        "cities": "jasidih"
    },
    {
        "state": "karnataka",
        "cities": "bail hongal"
    },
    {
        "state": "ladakh",
        "cities": "kargil"
    }
]

I have tried multiple code but adding this one as i am still trying to get my desired output

arr1.map((row, index) => ({
    itemLabel: arr1.state,
    itemValue: arr1.cities - arr2[index].cities
}))

Need this output

[
    {
        "state": "delhi",
        "cities": "central delhi"
    },
    {
        "state": "jharkhand",
        "cities": ["dumka","deoghar","jasidih"]
    },
    {
        "state": "karnataka",
        "cities": "bail hongal"
    },
    {
        "state": "ladakh",
        "cities": "kargil"
    }
]
1
  • What is the expected result of "central delhi" - "kargil" or arr1.cities - arr2[index].cities? Commented Oct 11, 2024 at 12:36

4 Answers 4

0

You could group first and then map the cities.

const
    data = [{ state: "delhi", cities: "central delhi" }, { state: "jharkhand", cities: "dumka" }, { state: "jharkhand", cities: "deoghar" }, { state: "jharkhand", cities: "jasidih" }, { state: "karnataka", cities: "bail hongal" }, { state: "ladakh", cities: "kargil" }],
    result = Object
        .entries(Object.groupBy(data, ({ state }) => state))
        .map(([state, objects]) => ({ state, cities: objects.map(({ cities }) => cities) }));

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

0

You can first use Object.groupBy to group all objects with the same state, then transform the result by mapping over its entries.

const arr = [
    {
        "state": "delhi",
        "cities": "central delhi"
    },
    {
        "state": "jharkhand",
        "cities": "dumka"
    },
    {
        "state": "jharkhand",
        "cities": "deoghar"
    },
    {
        "state": "jharkhand",
        "cities": "jasidih"
    },
    {
        "state": "karnataka",
        "cities": "bail hongal"
    },
    {
        "state": "ladakh",
        "cities": "kargil"
    }
];
const res = Object.entries(Object.groupBy(arr, o => o.state)).map(([state, els]) =>
   ({state, cities: els.length > 1 ? els.map(el => el.cities) : els[0].cities}));
console.log(res);

Comments

0

This can be done by grouping the cities by state and merging the cities for states that appear multiple times,This can be done using JS reduce .

const arr1 = [
    { "state": "delhi", "cities": "central delhi" },
    { "state": "jharkhand", "cities": "dumka" },
    { "state": "jharkhand", "cities": "deoghar" },
    { "state": "jharkhand", "cities": "jasidih" },
    { "state": "karnataka", "cities": "bail hongal" },
    { "state": "ladakh", "cities": "kargil" }
];

const result = Object.values(arr1.reduce((acc, { state, cities }) => {
    if (!acc[state]) {
        acc[state] = { state, cities: [] };
    }
    if (Array.isArray(acc[state].cities)) {
        acc[state].cities.push(cities);
    } else {
        acc[state].cities = [cities];
    }
    return acc;
}, {}));

result.forEach(entry => {
    if (entry.cities.length === 1) {
        entry.cities = entry.cities[0];
    }
});

console.log(result);

Comments

0

My suggestion:

const data = [{
    "state": "delhi",
    "cities": "central delhi"
  },
  {
    "state": "jharkhand",
    "cities": "dumka"
  },
  {
    "state": "jharkhand",
    "cities": "deoghar"
  },
  {
    "state": "jharkhand",
    "cities": "jasidih"
  },
  {
    "state": "karnataka",
    "cities": "bail hongal"
  },
  {
    "state": "ladakh",
    "cities": "kargil"
  }
];

// Group by state
const groupedData = Object.groupBy(data, item => item.state);

// Formatting the output
const result = Object.keys(groupedData).map(state => ({
  state,
  cities: [...new Set(groupedData[state].map(item => item.cities))] // Remove duplicates
}));

console.log(result);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.