Home > Mobile >  How can I mergen objects in an array
How can I mergen objects in an array

Time:09-21

I'm having some trouble with my array below I'd like to combine the objects that have the same timeslots.

I've tried looping through them and comparing all objects with eachothers timeslots. but (I think) because they are all seen as different instances it doesn't work.

I'm not asking for a full solution, but just some hints on how to compare these with eachother or how to approach this.

My array

[
    {
        "valid_days": ["0"],
        "timeslots": [
            {
                "from_time": "09:00",
                "to_time": "12:00"
            }
        ]
    },
    {
        "valid_days": ["1"],
        "timeslots": [
            {
                "from_time": "09:00",
                "to_time": "12:00"
            }
        ]
    },
    {
        "valid_days": ["2"],
        "timeslots": [
            {
                "from_time": "09:00",
                "to_time": "12:00"
            },
            {
                "from_time": "13:00",
                "to_time": "15:00"
            }
        ]
    },
    {
        "valid_days": ["3"],
        "timeslots": [
            {
                "from_time": "09:00",
                "to_time": "12:00"
            },
            {
                "from_time": "13:00",
                "to_time": "15:00"
            }
        ]
    }
]

Desired result

[
    {
        "valid_days": ["0", "1"],
        "timeslots": [
            {
                "from_time": "09:00",
                "to_time": "12:00"
            }
        ]
    },
    {
        "valid_days": ["2", "3"],
        "timeslots": [
            {
                "from_time": "09:00",
                "to_time": "12:00"
            },
            {
                "from_time": "13:00",
                "to_time": "15:00"
            }
        ]
    }
]

CodePudding user response:

As you only wanted to get some hints:

  1. For looping you should get familiar with the classic loops in JS or with the very nice to use build-in array functions:
  1. For comparing in JS you should always use the "===" or "!==" instead of the "==" or "!=". Both kinds don't support to deeply compare objects. That means { foo: 'bar' } === { foo: 'bar' } returns always false. So you have to compare yourself (maybe with a comparison function) the time-slot objects.

The rest should be straight-forward, I guess :)

CodePudding user response:

Finally I found the solution for you. can you check this once

const arr =[ { "valid_days": ["0"], "timeslots": [ {"from_time": "09:00","to_time": "12:00"}]},
  { "valid_days": ["1"],"timeslots": [{"from_time": "09:00","to_time": "12:00"}]},
  { "valid_days": ["2"],"timeslots": [{"from_time": "09:00","to_time": "12:00"},{"from_time": "13:00","to_time": "15:00"}]},
  { "valid_days": ["3"],"timeslots": [{"from_time": "09:00","to_time": "12:00"},{"from_time": "13:00","to_time": "15:00"}]}
]

const fun = (ar)=>{
 const output = ar.reduce((prev, curr) => {
  const tmp = prev.find(e => JSON.stringify(e.timeslots)==JSON.stringify(curr.timeslots));
    if (tmp) {
      tmp.valid_days.push(...curr.valid_days);
    } else {
      prev.push({
        timeslots: curr.timeslots,
        valid_days: [...curr.valid_days]
      });
    }
    return prev;
  }, []);
  return output
}
console.log(fun(arr))

can you check this once

  • Related