0

我正在尝试使用 nodejs 转换和映射/合并两个端点,我只知道现在学习 nodejs 的数据库。我需要使用 Nodejs 和 Promise 获得以下帮助

数据:

var a =  [ { farmer_revenue_id: 63,
      farmer_id: 518,
      activity_id: 0,
      revenue_type: 1,
      value_date: '2018-07-01',
      amount: '558.00',
      remarks: '',
      capture_date: '2018-07-17' }]
var b = [ { revenue_type_id: 1, name: 'Sale' }]

转换是在日期和金额上完成的。我们还将收入类型替换为收入类型名称。最后我们省略了一些不需要的数据。

预期输出:

[ { farmer_revenue_id: 63,
  farmer_id: 518,
  activity_id: 0,
  revenue_type_name: sale,
  value_date: '2018/07/01',
  amount: $ 558.00,
  capture_date: '2018/07/17' }]
4

1 回答 1

0

快速解决方法:

let a = [{ 
  farmer_revenue_id: 63,
  farmer_id: 518,
  activity_id: 0,
  revenue_type: 1,
  value_date: '2018-07-01',
  amount: '558.00',
  remarks: '',
  capture_date: '2018-07-17' 
}];

let b = [{ 
    revenue_type_id: 1, 
    name: 'Sale' 
}];

a = a.reduce((reduced, aItem) => {
    const aItemClone = {...aItem};
    const foundB = b.find((bItem) =>
        bItem.revenue_type_id === aItemClone.revenue_type
    ) || null;

    if (foundB !== null)
        aItemClone.revenue_type_name = foundB.name;
    delete aItemClone.revenue_type;
    reduced.push(aItemClone);

    return reduced;
}, []);

console.log(a);
于 2018-07-25T14:39:11.670 回答