简体   繁体   English

基于 object 属性在对象的数组中组合 Object

[英]Combine Object base on object property in array on objects

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.我只想在其中一个属性的基础上合并对象,还想在 output 中添加缺失的Days 名称

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.我也尝试了一些堆栈溢出的答案,但没有成功:-(一天的顺序无关紧要。提前致谢。

You could first create the 7 entries for the 7 days of the week, each with an empty array for the heure property.您可以首先为一周中的 7 天创建 7 个条目,每个条目都有一个用于heure属性的空数组。

Then iterate the original data, look up the right entry, and push the two times to the heure array.然后迭代原始数据,查找正确的条目,将两次推送到heure数组。

Note that your Day property has different spellings in your example input (DAY, Day).请注意,您的Day属性在示例输入中具有不同的拼写(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);

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; }

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>

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM