javascript 合并2个具有不同属性的对象数组

javascript 合并2个具有不同属性的对象数组

let a = [
	{
		currency: 'CNY',
		largeAipPurLimitAmt: '3',
		largeCvsnInLimitAmt: '2',
		largePurLimitAmt: '1',
		productName: '张三',
	},
	{
		currency: 'USD',
		largeAipPurLimitAmt: '6',
		largeCvsnInLimitAmt: '5',
		largePurLimitAmt: '4',
		productName: '李四',
	},
];
 let b= [
	{
		currency: 'CNY',
		productName: '张三',
	},
	{
		currency: 'USD',
		productName: '李四',
	},
	{
		currency: 'USD',
		productName: '王五',
	},
]; 

转换成c

let c = [
	{
		currency: 'CNY',
		largeAipPurLimitAmt: '3',
		largeCvsnInLimitAmt: '2',
		largePurLimitAmt: '1',
		productName: '张三',
	},
	{
		currency: 'USD',
		largeAipPurLimitAmt: '6',
		largeCvsnInLimitAmt: '5',
		largePurLimitAmt: '4',
		productName: '李四',
	},
	{
		currency: 'USD',
		productName: '王五',
	},
];

用productName 去匹配

let result = a.reduce((previousValue: any, currentValue: any) => {
	const needObj = b.find((o: any) => o.productName === currentValue.productName) ?? {};
	previousValue.push({ ...currentValue, ...needObj });
	return previousValue;
}, []);
console.log(result, 'ddddddd');

你可能感兴趣的:(javascript,前端,开发语言)