Javascript 对象数组和数组列表。需要得到组合;
我需要得到所有这些的组合 我有一个结构如下Javascript 对象数组和数组列表。需要得到组合;,javascript,algorithm,Javascript,Algorithm,我需要得到所有这些的组合 我有一个结构如下 [ { propertyId: 1, options: [ { id: 101, price: 100 }, { id: 102, price: 200 } ]
[
{
propertyId: 1,
options: [
{
id: 101,
price: 100
},
{
id: 102,
price: 200
}
]
},
{
propertyId: 2,
options: [
{
id: 201,
price: 300
},
{
id: 202,
price: 400
}
]
}
]
我需要得到所有属性的组合
结果:
[
{
price: 400,
properties: [
{
propertyId: 1,
optionId: 101,
},
{
propertyId: 2,
optionId: 201,
}
]
},
{
price: 500,
properties: [
{
propertyId: 1,
optionId: 101,
},
{
propertyId: 2,
optionId: 202,
}
]
},
{
price: 500,
properties: [
{
propertyId: 1,
optionId: 102,
},
{
propertyId: 2,
optionId: 201,
}
]
},
{
price: 600,
properties: [
{
propertyId: 1,
optionId: 102,
},
{
propertyId: 2,
optionId: 202,
}
]
}
以及所有其他组合(100+300、100+400、200+300、200+400…等)
我想我需要使用forof的递归循环。不幸的是,我还在考虑算法。有人能帮我吗。因为我们不使用PHP,所以正常的
for
对我们有好处:
const输入=[{
财产ID:1,
选项:[{
id:101,
价格:100
},
{
id:102,
售价:200
}
]
},
{
物业ID:2,
选项:[{
身份证号码:201,
价格:300
},
{
id:202,
售价:400
}
]
}
];
常量结果=[];
函数进程2属性(属性1、属性2){
对于(让物业1的期权1.期权){
对于(让物业2的期权2.期权){
结果:推({
价格:选项1.价格+选项2.价格,
特性:[
{propertyId:property1.propertyId,optionId:option1.id},
{propertyId:property2.propertyId,optionId:option2.id},
]
});
}
}
}
for(设i=0;i
您可能希望使用数组reduce方法我更喜欢使用for循环(比如for of)。因为它不是减少的任务。我想我需要递归循环,对于ofso,输出不是您需求的真实指示?如果您更喜欢使用for循环,请显示您已经尝试过的代码。我已经更新了输出。我不明白如何将输入操作为输出。您可以解释一下如何生成此输出吗