gpt4 book ai didi

javascript - 在javascript中合并多个数组?

转载 作者:行者123 更新时间:2023-12-05 00:28:14 25 4
gpt4 key购买 nike

我有这段代码应该基于匹配的 siteid 合并 3 个数组.
这在一定程度上起作用。基本上,它只找到 1 个站点标识并忽略相同的站点标识!
这是代码:
如果你运行下面的代码,你会看到它只添加了一个带有 的表单。 siteid 为 77 到合并的数组!但它应该将所有具有相同 siteid(本例中为 77)的表单添加到合并数组中。

var siteS1 = [{
Date: '2021-02-02 11:19',
siteid: 77,
},
{
Date: '2021-02-02 11:19',
siteid: 76,
},
{
Date: '2021-02-02 11:19',
siteid: 66,
},
{
Date: '2021-02-02 11:19',
siteid: 96,
}
];


var siteS2 = [{
Date: '2021-02-02 11:19',
siteid: 77,
},
{
Date: '2021-02-02 11:19',
siteid: 76,
},
{
Date: '2021-02-02 11:19',
siteid: 66,
},
{
Date: '2021-02-02 11:19',
siteid: 96,
}
];

var form = [{
data: {},
siteid: 77,
},
{
data: {},
siteid: 77,
},
{
data: {},
siteid: 77,
},
{
data: {},
siteid: 96,
}
];

const merge = (...arrayList) => {
const siteids = arrayList
.map(array => array.map(item => item.siteid))
.flat()
return Array.from(new Set(siteids))
}

const find = (siteid, array) => {
const found = array.find(item => item.siteid === siteid)
if (!found) return undefined // returns undefined if item was not found
const {
siteid: _,
...restFoundData
} = found // omit `siteid` from found item
return restFoundData
}

const populate = (siteids) => siteids.map(siteid => {
const foundSiteS1 = find(siteid, siteS1)
const foundSiteS2 = find(siteid, siteS2)
const foundForm = find(siteid, form)
return {
siteS1: foundSiteS1,
siteS2: foundSiteS2,
form: foundForm ? [foundForm.data] : [], // empty array if no items matched
}
})

const result = populate(merge(siteS1, siteS2, form))
console.log(result)
我在这里想念什么?
编辑:
这是示例代码:
var siteS1 = [{
Date: '2021-02-02 11:19',
siteid: 77,
}];


var siteS2 = [{
Date: '2021-02-02 11:19',
siteid: 77,
}];

var form = [{
data: {},
siteid: 77,
},
{
data: {},
siteid: 77,
},
{
data: {},
siteid: 77,
},
{
data: {},
siteid: 77,
},
{
data: {},
siteid: 78,
}];
预期的结果是这样的:
[{
"siteS1": {
"Date": "2021-02-02 11:19"
},
"siteS2": {
"Date": "2021-02-02 11:19"
},
"form": [{},{},{},{}]
}]
但是当前代码的结果是这样的:
[{
"siteS1": {
"Date": "2021-02-02 11:19"
},
"siteS2": {
"Date": "2021-02-02 11:19"
},
"form": [{}]
}]
如您所见,它只找到 1 个 siteid 为 77 的表单,而忽略其余的表单。

最佳答案

Array.find仅找到第一个匹配元素,我使用另一个函数来查找表单的所有匹配数据

const findAll = (siteid, array) => {
return array.filter(item => item.siteid === siteid).map(({siteid, ...data}) => data);
}

var siteS1 = [{
Date: '2021-02-02 11:19',
siteid: 77,
},
{
Date: '2021-02-02 11:19',
siteid: 76,
},
{
Date: '2021-02-02 11:19',
siteid: 66,
},
{
Date: '2021-02-02 11:19',
siteid: 96,
}
];


var siteS2 = [{
Date: '2021-02-02 11:19',
siteid: 77,
},
{
Date: '2021-02-02 11:19',
siteid: 76,
},
{
Date: '2021-02-02 11:19',
siteid: 66,
},
{
Date: '2021-02-02 11:19',
siteid: 96,
}
];

var form = [{
data: {},
siteid: 77,
},
{
data: {},
siteid: 77,
},
{
data: {},
siteid: 77,
},
{
data: {},
siteid: 96,
}
];

const merge = (...arrayList) => {
const siteids = arrayList
.map(array => array.map(item => item.siteid))
.flat()
return Array.from(new Set(siteids))
}

const find = (siteid, array) => {
const found = array.find(item => item.siteid === siteid)
if (!found) return undefined // returns undefined if item was not found
const {
siteid: _,
...restFoundData
} = found // omit `siteid` from found item
return restFoundData
}

const findAll = (siteid, array) => {
return array.filter(item => item.siteid === siteid).map(({siteid, ...data}) => data);
}

const populate = (siteids) => siteids.map(siteid => {
const foundSiteS1 = find(siteid, siteS1)
const foundSiteS2 = find(siteid, siteS2)
const foundForm = findAll(siteid, form)
return {
siteS1: foundSiteS1,
siteS2: foundSiteS2,
form: foundForm, // empty array if no items matched
}
})

const result = populate(merge(siteS1, siteS2, form))
console.log(result)

关于javascript - 在javascript中合并多个数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69840244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com