两个数组对象去重并且根据第一个数据生成一个新的对象?

作者站长头像
站长
· 阅读数 6

各位大佬,我想请问一下我想通过一个数组对象处理另外一个数组对象。生成一个新的数组对象要怎么处理?我想通过两个时间的字段做判断,给新生成的数组对象里面属性的值加上一个1。怎么写都不对。生成的都是有问题的。

通过的数组对象

thisDate: [{
            date: "12",
            prop: "twelve"
        },{
            date: "13",
            prop: "thirteen"
        },{
            date: "14",
            prop: "fourteen"
        },{
            date: "15",
            prop: "fifteen"
        },{
            date: "16",
            prop: "sixteen"
        },{
            date: "17",
            prop: "seventeen"
        },{
            date: "18",
            prop: "eighteen"
        }],

想要处理的数组对象

data: [{
            "DATE": "2023-09-04 15:33:54.0",
            "ROOMNAME": "测试会议室1",
            "ROOMID": "001"
        }, {
            "DATE": "2023-09-04 16:00:00",
            "ROOMNAME": "测试会议室1",
            "ROOMID": "001"
        }, {
            "DATE": "2023-09-04 17:00:00",
            "ROOMNAME": "测试会议室1",
            "ROOMID": "001"
        }, {
            "DATE": "2023-09-04 18:00:00",
            "ROOMNAME": "测试会议室1",
            "ROOMID": "001"
        }, {
            "DATE": "2023-09-04 19:00:00",
            "ROOMNAME": "测试会议室1",
            "ROOMID": "001"
        }, {
            "DATE": "2023-09-04 20:00:00",
            "ROOMNAME": "测试会议室1",
            "ROOMID": "001"
        }, {
            "DATE": "2023-09-04 21:00:00",
            "ROOMNAME": "测试会议室1",
            "ROOMID": "001"
        }, {
            "DATE": "2023-09-04 22:00:00",
            "ROOMNAME": "测试会议室1",
            "ROOMID": "001"
        }, {
            "DATE": "2023-09-04 23:00:00",
            "ROOMNAME": "测试会议室1",
            "ROOMID": "001"
        }, {
            "DATE": "2023-09-04 14:53:42.0",
            "ROOMNAME": "会议室2222",
            "ROOMID": "002"
        }, {
            "DATE": "2023-09-04 15:00:00",
            "ROOMNAME": "会议室2222",
            "ROOMID": "002"
        }, {
            "DATE": "2023-09-04 16:00:00",
            "ROOMNAME": "会议室2222",
            "ROOMID": "002"
        }, {
            "DATE": "2023-09-04 17:00:00",
            "ROOMNAME": "会议室2222",
            "ROOMID": "002"
        }, {
            "DATE": "2023-09-04 18:00:00",
            "ROOMNAME": "会议室2222",
            "ROOMID": "002"
        }, {
            "DATE": "2023-09-04 19:00:00",
            "ROOMNAME": "会议室2222",
            "ROOMID": "002"
        }, {
            "DATE": "2023-09-04 20:00:00",
            "ROOMNAME": "会议室2222",
            "ROOMID": "002"
        }]

我的思路有问题想不出来了,写的代码不对生成的有问题。

let list = [];
                    for (let i = 0; i < data.length; i++) {
                        let nextIndex = i + 1
                        let nextEle = data[nextIndex]
                        if (nextIndex < data.length) {
                            if (data[i].ROOMNAME != nextEle.ROOMNAME) {
                                let objData = {
                                    name: nextEle.ROOMNAME,
                                }
                                for (let a = 0; a < _this.thisDate.length; a++) {
                                    if (data[i].DATE.slice(11, 13) == _this.thisDate[a].date) {
                                        objData[_this.thisDate[a].prop] = '1';
                                    } else {
                                        objData[_this.thisDate[a].prop] = '';
                                    }
                                }
                                list.push(objData)
                            }
                        }
                    }
                    console.log('生成新的数组对象');
                    console.log(list);

我希望的是生成新的数组对象,合并同名,并且根据thisDate生成新的属性。在和data这个json判断时间对等的属性赋值1。

最后希望生成的格式格式是:

[{
            ROOMNAME: '测试会议室',
            twelve: '',
            thirteen: '',
            fourteen: '',
            fifteen: '',
            sixteen: '1',
            seventeen: '1',
            eighteen:'1',
        },{
            ROOMNAME: '会议室2222',
            twelve: '',
            thirteen: '',
            fourteen: '1',
            fifteen: '1',
            sixteen: '1',
            seventeen: '1',
            eighteen:'1',
        }]
回复
1个回答
avatar
test
2024-06-27

如果没有看错的话,就是记录一下各会议室在某个小时段是否有开会。说一下解法。

前两个数据是一样的:

const thisDate = [
  {
    date: "12",
    prop: "twelve",
  },
  {
    date: "13",
    prop: "thirteen",
  },
  {
    date: "14",
    prop: "fourteen",
  },
  {
    date: "15",
    prop: "fifteen",
  },
  {
    date: "16",
    prop: "sixteen",
  },
  {
    date: "17",
    prop: "seventeen",
  },
  {
    date: "18",
    prop: "eighteen",
  },
];
const data = [
  {
    DATE: "2023-09-04 15:33:54.0",
    ROOMNAME: "测试会议室1",
    ROOMID: "001",
  },
  {
    DATE: "2023-09-04 16:00:00",
    ROOMNAME: "测试会议室1",
    ROOMID: "001",
  },
  {
    DATE: "2023-09-04 17:00:00",
    ROOMNAME: "测试会议室1",
    ROOMID: "001",
  },
  {
    DATE: "2023-09-04 18:00:00",
    ROOMNAME: "测试会议室1",
    ROOMID: "001",
  },
  {
    DATE: "2023-09-04 19:00:00",
    ROOMNAME: "测试会议室1",
    ROOMID: "001",
  },
  {
    DATE: "2023-09-04 20:00:00",
    ROOMNAME: "测试会议室1",
    ROOMID: "001",
  },
  {
    DATE: "2023-09-04 21:00:00",
    ROOMNAME: "测试会议室1",
    ROOMID: "001",
  },
  {
    DATE: "2023-09-04 22:00:00",
    ROOMNAME: "测试会议室1",
    ROOMID: "001",
  },
  {
    DATE: "2023-09-04 23:00:00",
    ROOMNAME: "测试会议室1",
    ROOMID: "001",
  },
  {
    DATE: "2023-09-04 14:53:42.0",
    ROOMNAME: "会议室2222",
    ROOMID: "002",
  },
  {
    DATE: "2023-09-04 15:00:00",
    ROOMNAME: "会议室2222",
    ROOMID: "002",
  },
  {
    DATE: "2023-09-04 16:00:00",
    ROOMNAME: "会议室2222",
    ROOMID: "002",
  },
  {
    DATE: "2023-09-04 17:00:00",
    ROOMNAME: "会议室2222",
    ROOMID: "002",
  },
  {
    DATE: "2023-09-04 18:00:00",
    ROOMNAME: "会议室2222",
    ROOMID: "002",
  },
  {
    DATE: "2023-09-04 19:00:00",
    ROOMNAME: "会议室2222",
    ROOMID: "002",
  },
  {
    DATE: "2023-09-04 20:00:00",
    ROOMNAME: "会议室2222",
    ROOMID: "002",
  },
];

使用哈希表做一个前置处理,根据会议室名称进行分组,其值为该会议室开会的小时。

注意,此处比较随意。若是严谨一点,应当使用 ROOMID 作为 keyvalue 也应当使用 Set 作为容器,以免数据量大导致的重复问题。
const map = new Map();

for (const { DATE, ROOMNAME } of data) {
  map.set(ROOMNAME, [...(map.get(ROOMNAME) ?? []), DATE.slice(11, 13)]);
}

最后遍历哈希表,与 thisDate 对照一下,即可得到结果:

const list = [];
for (const [k, v] of map.entries()) {
  const obj = {
    name: k,
  };
  for (const { prop, date } of thisDate) {
    obj[prop] = v.includes(date) ? "1" : "";
  }
  list.push(obj);
}

console.log("生成新的数组对象");
console.log(list);

结果如下:

生成新的数组对象
[
  {
    name: '测试会议室1',
    twelve: '',
    thirteen: '',
    fourteen: '',
    fifteen: '1',
    sixteen: '1',
    seventeen: '1',
    eighteen: '1'
  },
  {
    name: '会议室2222',
    twelve: '',
    thirteen: '',
    fourteen: '1',
    fifteen: '1',
    sixteen: '1',
    seventeen: '1',
    eighteen: '1'
  }
]
回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容