OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

group array of object with common ids and concatenate their other properties using javascript

  • Thread starter Thread starter Aren Trot
  • Start date Start date
A

Aren Trot

Guest
I am trying to achieve a pretty complex iteration but havent been successful so far using reduce. Here is a small example of it

The input is as follows

Code:
let input = {
    "data": [
        {
            "state": "CA",
            "city": [ "Mountain View"],
            "metadata": { "id": 15 },
        },
        {
            "state": "TX",
            "city": [ "Austin" ],
            "metadata": { "id": 15 },
        },
        {
            "state": "AZ",
            "city": [ "Flagstaff" ],
            "metadata": { "id": 10 },
        },
    ],
}

As you can clearly see, the id is repeated twice in 2 objects. i want to concatenate these two objects into one when id is same. so if there are 3 objects with same id, i want to concatenate 3 objects as one. Apart from this, i also want to concatenate the state an city property when the id is matched.

So my final output should look like this

Code:
let result = {
    "data": [
        {
            "state": ["CA", "TX"],
            "city": ["Mountain View", "Austin"],
            "metadata": { "id": 15 },
        },
        {
            "state": "AZ",
            "city": [ "Flagstaff" ],
            "metadata": { "id": 10 },
        },
    ],
}

can someone please let me know how to precisely achieve this. Here is how i have tried using reduce but i could not get the expected end result.

Code:
let result = Object.values(input?.data?.reduce((acc, {state, city, metadata}) => {
    if (!acc[metadata?.id]) {
        acc[metadata?.id] = { id };
    }
    acc[metadata?.id].state = acc[metadata?.id].state.concat(state);
    acc[metadata?.id].city = acc[metadata?.id].city.concat(city);
    return acc;
}, {}));

<p>I am trying to achieve a pretty complex iteration but havent been successful so far using reduce. Here is a small example of it</p>
<p>The input is as follows</p>
<pre><code>let input = {
"data": [
{
"state": "CA",
"city": [ "Mountain View"],
"metadata": { "id": 15 },
},
{
"state": "TX",
"city": [ "Austin" ],
"metadata": { "id": 15 },
},
{
"state": "AZ",
"city": [ "Flagstaff" ],
"metadata": { "id": 10 },
},
],
}
</code></pre>
<p>As you can clearly see, the <code>id</code> is repeated twice in 2 objects. i want to concatenate these two objects into one when id is same. so if there are 3 objects with same id, i want to concatenate 3 objects as one. Apart from this, i also want to concatenate the <code>state</code> an <code>city</code> property when the id is matched.</p>
<p>So my final output should look like this</p>
<pre><code>let result = {
"data": [
{
"state": ["CA", "TX"],
"city": ["Mountain View", "Austin"],
"metadata": { "id": 15 },
},
{
"state": "AZ",
"city": [ "Flagstaff" ],
"metadata": { "id": 10 },
},
],
}
</code></pre>
<p>can someone please let me know how to precisely achieve this. Here is how i have tried using reduce but i could not get the expected end result.</p>
<pre><code>let result = Object.values(input?.data?.reduce((acc, {state, city, metadata}) => {
if (!acc[metadata?.id]) {
acc[metadata?.id] = { id };
}
acc[metadata?.id].state = acc[metadata?.id].state.concat(state);
acc[metadata?.id].city = acc[metadata?.id].city.concat(city);
return acc;
}, {}));
</code></pre>
 

Latest posts

Top