combine values to an array with same id For JSON object

javascript merge objects with same key
javascript merge array of objects by key
json join by key
merge two json objects javascript
merge two json objects node js
javascript merge key value pairs
merge two json arrays javascript
put multiple json object into array javascript

after join operation, I get JSON object with duplicate id values how it is possible to map them with their association property to an array with javascript

{
    "id": 1,
    "name": "doc 1",
    "appointmentTime": "2018-12-28T00:00:43"
},

{
    "id": 2,
    "name": "doc 2",
    "appointmentTime": "2018-12-25T23:00:53"
},
{
    "id": 2,
    "name": "doc 2",
    "appointmentTime": "2018-12-26T23:00:02"
},
{
    "id": 3,
    "name": "doc3",
    "appointmentTime": null
},

I want something like that

{
    "id": 1,
    "name": "doc1",
    "appointmentTime": ["2018-12-28T00:00:43"]
},
{
        "id": 2,
        "name": "doc 2",
        "appointmentTime": ["2018-12-26T23:00:02","2018-12-26T23:00:02"]
    },
    {
        "id": 3,
        "name": "doc3",
        "appointmentTime": null
    },

You will need reduce function for that:

const src = [{
    "id": 1,
    "name": "doc 1",
    "appointmentTime": "2018-12-28T00:00:43"
},

{
    "id": 2,
    "name": "doc 2",
    "appointmentTime": "2018-12-25T23:00:53"
},
{
    "id": 2,
    "name": "doc 2",
    "appointmentTime": "2018-12-26T23:00:02"
},
{
    "id": 3,
    "name": "doc3",
    "appointmentTime": null
}]

const result = src.reduce((acc, {id, name, appointmentTime}) => {
  const existing = acc.find(i => i.id === id)
  if (existing) { existing.appointmentTime.push(appointmentTime) } 
  else {acc.push({id, name, appointmentTime: [appointmentTime]})}
  
  return acc
}, [])

console.log(result)

How to merge two json object values by id with plain Javascript (ES6 , You could use Map and Object.assign for merging the objects. var request1 = [{ ObjId: 174864, ObjMutationD: "2010-07-09T00:00:00.000Z", ObjMitarbeiterS:  Hi, I have a json object that contains some string parameters that have multiple values inside like, "test1,test2,test3". My problem is that i have three of those that needs to be combined i some way.

> let mergeDuplicates = (arr, uniqueKey, mergeKey) => {
> let hashMap = {}; arr.forEach(item => {
> 
>     if(hashMap[item[uniqueKey]] == null) { item[mergeKey] = [item[mergeKey]]; hashMap[item[uniqueKey]] = item;  }
>     else { hashMap[item[uniqueKey]][mergeKey].push(item[mergeKey]); }

>     let ansArr = [];
>     for(var key in hashMap){
>        ansArr.push(hashMap[key]);
>     }
>     return ansArr;
> 
> })
>
> }
> myarray = mergeDuplicates(myarray, "id", "appointmentTime");

How to Optimize Merge of Two Objects That Include Arrays of , Object values need to be remerged. else if(obj[i] != null && utils. If you test of an object, then for an array, what falls through should be a primitve. convert to JSON, just reference objects where possible function mergeObjects(target, index of objects in target object using ID property // Assume if target has same named  same - merge two json arrays in javascript Merge JSON Object with same “key” and add their “value” using JavaScript (4)

i would do it like this:

    //lets say you got your jsonObj already in an Array like:
var myarray = [{
    "id": 1,
    "name": "doc 1",
    "appointmentTime": "2018-12-28T00:00:43"
  },
  {
    "id": 2,
    "name": "doc 2",
    "appointmentTime": "2018-12-25T23:00:53"
  },
  {
    "id": 2,
    "name": "doc 2",
    "appointmentTime": "2018-12-26T23:00:02"
  },
  {
    "id": 3,
    "name": "doc3",
    "appointmentTime": null
  }
];

//just loop through your data and combine it
for (var i = 0; i < myarray.length; i++) {
  //And loop again for duplicate data
  for (var j = i + 1; j < myarray.length; j++) {
    if (myarray[i].id == myarray[j].id) {
      var tmp = myarray[j].appointmentTime;
      myarray[j].appointmentTime = [];
      myarray[j].appointmentTime.push(tmp);
      myarray[j].appointmentTime.push(myarray[i].appointmentTime);

      myarray[i] = {};
    }
  }
}
console.log(myarray);

remove afterwards all empty jsonobj

https://jsfiddle.net/m073qwr6/1/

How to merge content of two or more objects in JavaScript?, object or array, it will be completely overwritten by a property with the same key in the second or subsequent object. The values are not merged. This can be seen in the example below by examining the value of banana. Assuming that you would like to merge two JSON arrays like below: var json1 = [{id:1, name: 'xxx​' . When using FOR JSON PATH to turn a result set into a JSON string, SQL Server will automatically add square brackets around the JSON output as if it were an array. This is a problem in our scenario because when we use FOR JSON PATH to turn the Car and Toy table into JSON strings, we eventually want to combine them together into the same array

array_unique - Manual, 5, PHP 7). array_unique — Removes duplicate values from an array You can make it unique for any field like id, name or num. My object unique function: But I need the JSON keys ('User ID 1', 'User ID 3', 'User ID 3') not values so the logic won't work for my scenario. And even if you put JSON object inside an array you will get same JSON Object while iterating, you won't get the JSON keys or values. so solution won't work.

Combining Data, Combining or merging data may involve one of the following tasks: here modifies the articles array in place by adding a new key-value-pair for brand . var result = brands.filter(function(brand) { return brand.id === article.brand_id; }); delete two or more data sets that contain attributes describing the same observations,  const obj = { a: 1 }; const copy = Object.assign({}, obj); console.log(copy); // { a: 1 } Warning for Deep Clone. For deep cloning, we need to use alternatives, because Object.assign() copies property values. If the source value is a reference to an object, it only copies the reference value.

Combine two arrays based on a common key value pair,, Hello, I am trying to combine two arrays in Dataweave based on emplyeeID and grab the other elements from both arrays. Example Array 1: [{. emplyeeID = 1,. The JSONArray can parse text from a String to produce a vector-like object and supports java.util.List interface. We can use org.json.simple.JSONArray class to merge two JSON arrays in Java. We can merge two JSON arrays using the addAll () method (inherited from interface java.util.List) in the below program.

Comments
  • Duplicate of stackoverflow.com/questions/24302630/…
  • Thanks, is util. I create some changes for other array call 'formData2'... var result = formData2.reduce((acc, {name, value}) => { var existing = acc.find(i => i.name === name); if(existing){ existing.value = (existing.value + ';' + value); }else{ acc.push({name, value}); }return acc; }, [])
  • This will have O(n) complexity.