0

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
    },
1

3 Answers 3

4

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)

I also used destructuring assignment

Sign up to request clarification or add additional context in comments.

1 Comment

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; }, [])
1
> 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");

1 Comment

This will have O(n) complexity.
0

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.