Home > Blockchain >  How can i merge an array of objects in Nodejs with underscore
How can i merge an array of objects in Nodejs with underscore

Time:11-03

In my case i have a 2 db queries which are objects, one which returns all possible items which consists of a key and name field and then the other is a object which has the key, name and value field. What i am trying to do is to merge both objects where object 1 is the main object and object 2 should be merged into it.

What i ideally want is to return all items in Data2 with the value field merged into data2 and 0 if there is no data in data 1. if thats not possible i would be ok with no value in items in data 2 but i get even a strange result for that.

Fyi i am using underscore

const data1 = [
        {
            "count": 2,
            "key": "c28f7ead-d87b-4ad5-b6b3-1f204b013b50",
            "name": "Notes Written"
        },
        {
            "count": 1,
            "key": "d0181c74-22a9-4f99-9cc9-df3467c51805",
            "name": "Pop-Bys Delivered"
        },
        {
            "count": 2,
            "key": "90d142ea-6748-4781-b2b9-4f05aab12956",
            "name": "Database Additions"
        },
        {
            "count": 1,
            "key": "723e95dd-8c47-48ed-b9c3-1b010b092a1b",
            "name": "Referals Given"
        }
 
    ]

const data2 = [
                    {
                        "key": "8646ec5d-7a72-49bd-9a68-cf326d1c4a14",
                        "name": "Calls Made"
                    },
                    {
                        "key": "c28f7ead-d87b-4ad5-b6b3-1f204b013b50",
                        "name": "Notes Written"
                    },
                    {
                        "key": "d0181c74-22a9-4f99-9cc9-df3467c51805",
                        "name": "Pop-Bys Delivered"
                    },
                    {
                        "key": "90d142ea-6748-4781-b2b9-4f05aab12956",
                        "name": "Database Additions"
                    },
                    {
                        "key": "723e95dd-8c47-48ed-b9c3-1b010b092a1b",
                        "name": "Referals Given"
                    },
                    {
                        "key": "0f054686-ef13-4993-ac5b-f640ceeaaa8d",
                        "name": "Referals Received"
                    }
                ]

 console.log(_.extend( data2, data1 ))

Here is a Replit example Sample Code

CodePudding user response:

  • Using reduce, iterate over data1 while updating a Map of key-count pairs
  • Using each, iterate over data2 and set the value to the value of the key from the map, or 0 if it doesn't exist

const 
  data1 = [ { "count": 2, "key": "c28f7ead-d87b-4ad5-b6b3-1f204b013b50", "name": "Notes Written" }, { "count": 1, "key": "d0181c74-22a9-4f99-9cc9-df3467c51805", "name": "Pop-Bys Delivered" }, { "count": 2, "key": "90d142ea-6748-4781-b2b9-4f05aab12956", "name": "Database Additions" }, { "count": 1, "key": "723e95dd-8c47-48ed-b9c3-1b010b092a1b", "name": "Referals Given" } ],
  data2 = [ { "key": "8646ec5d-7a72-49bd-9a68-cf326d1c4a14", "name": "Calls Made" }, { "key": "c28f7ead-d87b-4ad5-b6b3-1f204b013b50", "name": "Notes Written" }, { "key": "d0181c74-22a9-4f99-9cc9-df3467c51805", "name": "Pop-Bys Delivered" }, { "key": "90d142ea-6748-4781-b2b9-4f05aab12956", "name": "Database Additions" }, { "key": "723e95dd-8c47-48ed-b9c3-1b010b092a1b", "name": "Referals Given" }, { "key": "0f054686-ef13-4993-ac5b-f640ceeaaa8d", "name": "Referals Received" } ];

const map = _.reduce(
  data1, 
  (map, { key, count }) => map.set(key, count),
  new Map
);

_.each(
  data2,
  e => e.value = map.get(e.key) || 0
);

console.log(data2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.13.1/underscore-min.js" integrity="sha512-ZuOjyqq409 q6uc49UiBF3fTeyRyP8Qs0Jf/7FxH5LfhqBMzrR5cwbpDA4BgzSo884w6q/ oNdIeHenOqhISGw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related