【发布时间】:2017-11-27 20:20:43
【问题描述】:
嗨,实际上我有两个数组(服务和报价),我正在尝试使用 reduce 方法根据服务 id 将报价数组与服务数组连接起来,我尝试了不同的方法但没有任何效果我无法解决这个检查我的js fiddle link(无法连接到报价阵列)。请帮我解决这个问题。提前致谢
const services = [
{
cid:1,
catname:'Facial',
services:[
{
sid:30,
sname:'Fruit facial'
},
{
sid:33,
sname:'Herbal facial'
}
]
},
{
cid:2,
catname:'Massage',
services:[
{
sid:40,
sname:'Head Massage'
},
{
sid:45,
sname:'Back Massage'
},
{
sid:46,
sname:'Face Massage'
}
]
}
]
提供数组 - 这里基于服务 ID (sid) 我想连接服务数组并创建新数组
const offer = [
{
offid:1,
sid:'33,40'
offvalue : 10%
},
{
offid:2,
sid:'45,46',
offvalue : 100
}
]
预期结果:
const Result = [
{
cid:1,
catname:'Facial',
services:[
{
sid:30,
sname:'Fruit facial'
},
{
sid:33,
sname:'Herbal facial',
offid:1,
offvalue : 10%
}
]
},
{
cid:2,
catname:'Massage',
services:[
{
sid:40,
sname:'Head Massage',
offid:1,
offvalue : 10%
},
{
sid:45,
sname:'Back Massage',
offid:2,
offvalue : 100
},
{
sid:46,
sname:'Face Massage',
offid:2,
offvalue : 100
}
]
}
]
【问题讨论】:
-
你试过什么?请将您的代码显示为minimal reproducible example。
-
@Andy 嗨,请检查我的链接jsfiddle.net/kg6981ce 我试过了,因为我是 JavaScript 新手,所以无法完成
标签: javascript arrays ecmascript-6 reduce