使用 JavaScript ES6 Reduce 方法基于 2 个数组创建结果数组
Creating result array based on 2 arrays using JavaScript ES6 Reduce method
你好,实际上我有两个数组(服务和报价),我正在尝试使用 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
}
]
}
]
如果您可以使用 ES6 功能,那么这应该可行:
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'
}],
}];
const offer = [{
offid:1,
sid:'33,40',
offvalue : "10%"
}, {
offid:2,
sid:'45,46',
offvalue : "100"
}];
const result = services.map(val => {
return {
...val,
services: val.services.map(serv => {
// Here is where we find any matching offers, then add their data to the service.
const off = offer.find(x => x.sid.split(",").includes(String(serv.sid)));
if (off) {
serv.offid = off.offid;
serv.offvalue = off.offvalue;
}
return serv;
}),
}
});
console.log(result);
你好,实际上我有两个数组(服务和报价),我正在尝试使用 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
}
]
}
]
如果您可以使用 ES6 功能,那么这应该可行:
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'
}],
}];
const offer = [{
offid:1,
sid:'33,40',
offvalue : "10%"
}, {
offid:2,
sid:'45,46',
offvalue : "100"
}];
const result = services.map(val => {
return {
...val,
services: val.services.map(serv => {
// Here is where we find any matching offers, then add their data to the service.
const off = offer.find(x => x.sid.split(",").includes(String(serv.sid)));
if (off) {
serv.offid = off.offid;
serv.offvalue = off.offvalue;
}
return serv;
}),
}
});
console.log(result);