各位大佬,我想请问一下我想通过一个数组对象处理另外一个数组对象。生成一个新的数组对象要怎么处理?我想通过两个时间的字段做判断,给新生成的数组对象里面属性的值加上一个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', }]
如果没有看错的话,就是记录一下各会议室在某个小时段是否有开会。说一下解法。
前两个数据是一样的:
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
作为key
,value
也应当使用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' }]
问题内容: 我有两个数组: 阵列1: 和数组2: 我需要基于合并这两个数组,并获得此: 我如何在不迭代低谷的情况下做到这一点? 问题答案: 你可以这样- 如果和的顺序不同,请使用以下代码: 如果和以相同的顺序使用
如何把这个数据 转为
使用提供函数生成的键与提供的对象相同的值创建一个对象。 使用 Object.keys(obj) 来迭代对象的键。 使用 Array.reduce() 创建一个具有相同值的新对象,并使用 fn 来映射键。 const mapKeys = (obj, fn) => Object.keys(obj).reduce((acc, k) => { acc[fn(obj[k], k, obj)] =
我有2个对象数组和2个如果条件 编辑网格行时,和将具有相同的长度,因此第一个if条件执行。当我添加新行时,长度将超过,因此第二个如果条件执行。 但我有一种情况,用户可以添加一个新行,然后立即编辑一个现有行。在那个特定的场景中,我需要执行if条件,并且我停留在那里。我们将听取任何建议。谢谢
test的之和为15; 假如我总数为29, 生成15个框(根据test的之和15就生成15个框)的数据:如下图() 下面写的这个方法:在第14个框的值为27-29之间的数据,第15个框的值为负数了,请问这个方法能优化下吗?或者还有其他方法吗?