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

各位大佬,我想请问一下我想通过一个数组对象处理另外一个数组对象。生成一个新的数组对象要怎么处理?我想通过两个时间的字段做判断,给新生成的数组对象里面属性的值加上一个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 作为 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'

}

]

以上是 两个数组对象去重并且根据第一个数据生成一个新的对象? 的全部内容, 来源链接: utcz.com/p/934874.html

回到顶部