用另一个数组的数据替换一个数组的数据
2020-09-02
59
我有一个数组
const data = [
{
id: 0,
identifier: 'free-2020',
name: '<I18nText id="pricing.plans.name.free" />',
planTitle: 'free',
price: '00',
freeSearches: '<I18nText id="pricing.plans.features.unlimited" />',
freeBookings: '<I18nText id="pricing.plans.features.unlimited" />',
travelerProfiles: '<I18nText id="pricing.plans.features.unlimited" />',
supportTickets: '<I18nText id="pricing.plans.features.available" />',
supportByPhone: ' - ',
supplierChannels: ' - ',
customDomain: ' - ',
active: true,
},
{
id: 1,
identifier: 'basic-2020',
name: '<I18nText id="pricing.plans.name.basic" />',
planTitle: 'basic',
price: '29',
freeSearches: '<I18nText id="pricing.plans.features.unlimited" />',
freeBookings: '<I18nText id="pricing.plans.features.unlimited" />',
travelerProfiles: '<I18nText id="pricing.plans.features.unlimited" />',
supportTickets: '<I18nText id="pricing.plans.features.available" />',
supportByPhone: ' - ',
supplierChannels: ' 1 ',
customDomain: ' - ',
active: false,
},
{
id: 2,
identifier: 'standard-2020',
name: '<I18nText id="pricing.plans.name.standard" />',
planTitle: 'standard',
price: '59',
freeSearches: '<I18nText id="pricing.plans.features.unlimited" />',
freeBookings: '<I18nText id="pricing.plans.features.unlimited" />',
travelerProfiles: '<I18nText id="pricing.plans.features.unlimited" />',
supportTickets: '<I18nText id="pricing.plans.features.available" />',
supportByPhone: ' 1hr/mo ',
supplierChannels: ' 2 ',
customDomain: ' - ',
active: false,
},
{
id: 3,
identifier: 'professional-2020',
name: '<I18nText id="pricing.plans.name.professional" />',
planTitle: 'professional',
price: '99',
freeSearches: '<I18nText id="pricing.plans.features.unlimited" />',
freeBookings: '<I18nText id="pricing.plans.features.unlimited" />',
travelerProfiles: '<I18nText id="pricing.plans.features.unlimited" />',
supportTickets: '<I18nText id="pricing.plans.features.available" />',
supportByPhone: ' 3hr/mo ',
supplierChannels: ' 5 ',
customDomain: 'Yes',
active: false,
},
{
id: 4,
identifier: 'custom-2020',
name: '<I18nText id="pricing.plans.name.custom" />',
planTitle: 'custom',
price: ' - ',
freeSearches: '<I18nText id="pricing.plans.features.unlimited" />',
freeBookings: '<I18nText id="pricing.plans.features.unlimited" />',
travelerProfiles: '<I18nText id="pricing.plans.features.unlimited" />',
supportTickets: '<I18nText id="pricing.plans.features.available" />',
supportByPhone: '<I18nText id="pricing.plans.features.unlimited" />',
supplierChannels: '<I18nText id="pricing.plans.features.unlimited" />',
customDomain: '<I18nText id="pricing.plans.features.available" />',
active: false,
},
];
和另一个数组
const plans = [
{id: 2, identifier: "professional-2020", name: "professional", currencyCode: "usd", price: "99.0"},
{id: 3, identifier: "free-2020", name: "free", currencyCode: "usd", price: "0.0"},
{id: 4, identifier: "basic-2020", name: "basic", currencyCode: "usd", price: "29.0"},
{id: 5, identifier: "standard-2020", name: "standard", currencyCode: "usd", price: "59.0"},
{id: 6, identifier: "custom-2020", name: "custom", currencyCode: "usd", price: "999.0"},
]
现在我想通过匹配
identifier
键将
plans
数组中的数据放入
data
数组中。因此
data
数组的
id
、
identifier
、
name
、
price
数据将被
plans
数组的这些键的值替换。似乎我需要将这两个数组中的这些数据映射到另一个数组?或者可能不需要。我该如何实现此功能。需要代码示例。
3个回答
data = data.map(d => {
const correspondingPlan = plans.find(plan => d.identifier === plan.identifier);
if (correspondingPlan) {
return {...d, ...correspondingPlan};
} else {
return d
}
})
编辑:IE 不支持
Array.prototype.find
。以下是使用过滤器的解决方案:
data = data.map(d => {
const correspondingPlan = plans.filter(plan => d.identifier === plan.identifier)[0];
if (correspondingPlan) {
return {...d, ...correspondingPlan};
} else {
return d
}
})
Tur1ng
2020-09-02
const plansById = {};
plans.forEach((plan) => {
plansById[plan.identifier] = plan;
});
data.forEach((line) => {
line.plan = plansById[line.planTitle];
// You should probably add some null-check in case of a missing or wrong planTitle
});
xurei
2020-09-02
您可以
reduce
来生成预期结果。
data.reduce((acc, curr) => {
const found = plans.find(p => p.identifier === curr.identifier)
if(found) {
return [...acc, {...curr, ...found}];
} else {
return [...acc, { ...curr }]
}
return acc;
}, [])
Hamza Zaidi
2020-09-02