Wednesday 24 May 2017

combine javascript objects with inner array with same key (similar to foriegn key )

           Obj1 = [
                {
                    'id':1,
                    'name':'apple'
                },
                {
                    'id':2,
                    'name':'mango'
                }
            ]

            Obj2 = [
                {
                    'parentId' : 1,
                    'time': [1,2,3]
                },
                {
                    'parentId' : 1,
                    'time': [4,5,6]
                },
                {
                    'parentId' : 1,
                    'time': [7,8,9]
                },
                {
                    'parentId' : 2,
                    'time': [11,12]
                }
            ]


            result = [
            {
                    'id':1,
                    'name':'apple',
                    'time': [1,2,3,4,5,6,7,8,9]
                },
                {
                    'id':2,
                    'name':'mango',
                    'time': [11,12]
                },
            ]

How to combine these two objects who have same key in common with NodeJs or Javascript and get the following resulting object.

without using Jquery or any other framework.

Note : NodeJs application



via K.Karthik

No comments:

Post a Comment