0

I just want to merge the objects on the base of one of the properties also want to add missing Days name in the output.

Take this for an example:

var array =  [
       {
      "heure1": "14:00",
      "heure2": "17:00",
      "day": "Sunday",
    },
       {
      "heure1": "08:00",
      "heure2": "13:00",
      "day": "Sunday",
    },
       {
      "heure1": "14:00",
      "heure2": "16:00",
      "day": "Monday",
    },
       {
      "heure1": "08:00",
      "heure2": "18:00",
      "day": "Monday",
    },
  ];

Expected result:

var array =  [
       {
      "heure": ["14:00","17:00","08:00","13:00"],
      "day": "Sunday",
    },
     {
      "heure": ["14:00","16:00","08:00","18:00"],
      "day": "Monday",
    },
    {
      "heure": [],
      "day": "Saturday",
    },
    {
      "heure": [],
      "day": "Friday",
    },
    {
      "heure": [],
      "day": "Thursday",
    },
    {
      "heure": [],
      "day": "Wednesday",
    },
    {
      "heure": [],
      "day": "Tuesday",
    },

  ];

Im also trying some of stack overflow answers but Not getting success :-( Order of the day dose not matter. Thanks in advance.

3
  • Please share your attempt and where you are stuck. Commented Aug 10, 2020 at 17:43
  • BTW, that is a strange order of days in your desired output... What is the logic? Commented Aug 10, 2020 at 17:45
  • order of days does not matter Commented Aug 10, 2020 at 17:52

4 Answers 4

1

My try

<script>
    var array = [{
            "heure1": "14:00",
            "heure2": "17:00",
            "DAY": "Sunday",
        },
        {
            "heure1": "08:00",
            "heure2": "13:00",
            "DAY": "Sunday",
        },
        {
            "heure1": "14:00",
            "heure2": "16:00",
            "DAY": "Monday",
        },
        {
            "heure1": "08:00",
            "heure2": "18:00",
            "DAY": "Monday",
        },
    ];
    var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Sunday", "Saturday"];
    var result = [];
    days.map(function (day) {
        var daysInArray = array.filter(function (a) {
            return day == a.DAY
        })
        // console.log(daysInArray);
        if (daysInArray.length) {
            time = [];
            daysInArray.map(function (item, i) {
                for (var key in item) {
                    if (daysInArray[0].hasOwnProperty(key) && key != "DAY") {
                        time.push(item[key])
                    }
                }
            })
            result.push({
                "day": day,
                "heure": time
            })
        } else {
            result.push({
                "day": day,
                "heure": []
            })
        }
    })
    console.log(result);
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much this is more dynamic then others and also very easy to understand.
@osamasomy this verifies the adage: why want to keep it simple when you can do it complicated ...
1

You could first create the 7 entries for the 7 days of the week, each with an empty array for the heure property.

Then iterate the original data, look up the right entry, and push the two times to the heure array.

Note that your Day property has different spellings in your example input (DAY, Day). I would strongly suggest to use all lowercase for such property names.

Here is an implementation:

var array =  [{"heure1": "14:00","heure2": "17:00","day": "Sunday",}, {"heure1": "08:00","heure2": "13:00","day": "Sunday",}, {"heure1": "14:00","heure2": "16:00","day": "Monday",}, {"heure1": "08:00","heure2": "18:00","day": "Monday", },];

let days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
let obj = Object.fromEntries(days.map(day => [day, { heure: [], day }]));
for (let {heure1, heure2, day} of array) obj[day].heure.push(heure1, heure2);
let result = Object.values(obj);

console.log(result);

6 Comments

What about this answer? Does it suit your needs?
Yes this is very help full but one of the guy send me more dynamic answer. by the Thanks for fast response I really appreciate. Thanks.
I don't see how that is more dynamic, but OK. Note it iterates the input several times, this solution only iterations the input once.
His answer not depend on the key values like heure1 if some data is changed, I think it should work.
by the way I really appreciate your Answer as well.
|
1

my way...

var arr_1 = 
    [ { heure1: '14:00', heure2: '17:00', day: 'Sunday' } 
    , { heure1: '08:00', heure2: '13:00', day: 'Sunday' } 
    , { heure1: '14:00', heure2: '16:00', day: 'Monday' } 
    , { heure1: '08:00', heure2: '18:00', day: 'Monday' } 
    ] 

const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']

const res = days.map(d=>
        {
        let r = { heure:[], day:d }
        arr_1.filter(x=>x.day===d)
             .forEach(({heure1,heure2})=> { r.heure.push(heure1,heure2) })
        r.heure.sort() 
        return r
        })


console.log( res  )
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

0

Is that result structure necessary?

If not, modifying the result structure, you could do something like this:

const getHeureByDay = (heureArray) => {
  let result = {
    Sunday: { heure: [] },
    Monday: { heure: [] },
    Tuesday: { heure: [] },
    Wednesday: { heure: [] },
    Thursday: { heure: [] },
    Friday: { heure: [] },
    Saturday: { heure: [] },
  };
  
  heureArray.forEach((heureItem) => {
    Object.keys(heureItem).forEach((key) => {
      if (key !== "day") {
        result[heureItem.day].heure.push(heureItem[key]);
      }
    })
  });
  
  return result;
};

const heureArray = [
  {
    "heure1": "14:00",
    "heure2": "17:00",
    "day": "Sunday",
  },
  {
    "heure1": "08:00",
    "heure2": "13:00",
    "day": "Sunday",
  },
     {
    "heure1": "14:00",
    "heure2": "16:00",
    "day": "Monday",
  },
     {
    "heure1": "08:00",
    "heure2": "18:00",
    "day": "Monday",
  }
];

console.log(getHeureByDay(heureArray));

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.