skip to Main Content

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"
    }
]

4

Answers


  1. 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);
    Login or Signup to reply.
  2. 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);
    Login or Signup to reply.
  3. 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);
    Login or Signup to reply.
  4. 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);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search