javascript - REST API(React.js)中两个json对象的付款历史

标签 javascript reactjs rest

我正在使用React.js(前端)和Node.js(Express.js)作为后端来处理我的私人银行项目。我正在制作付款记录,需要帮助。

我创建了两条路线。其中一个返回我进行的转账,第二个返回我收到的转账。

我想将这两个JSON对象合并为一个,并在React.js组件中按日期对它们进行排序。 axios是否允许这种可能性?

我还将代码留给控制器,也许有人对如何更好地显示付款历史有更好的主意?

控制器REST API:

exports.findAllByIdRecipient = (req, res) => {
  Transaction.findAll({
    where: {
      id_recipient: req.params.transactionRecipientId,
    },
  }).then(bill => {
    res.send(bill);
  });
};

exports.findAllByIdSender = (req, res) => {
  Transaction.findAll({
    where: {
      id_sender: req.params.transactionSenderId,
    },
  }).then(bill => {
    res.send(bill);
  });
};


路线:

app.get('/api/transactions/recipient/:transactionRecipientId', transactions.findAllByIdRecipient);
app.get('/api/transactions/sender/:transactionSenderId', transactions.findAllByIdSender);


来自../recipient/1的JSON:

[
    {
        "id": 3,
        "id_sender": 2,
        "id_recipient": 1,
        "data_time": "2018-12-08T03:49:00.000Z",
        "amount_money": 30,
        "transfer_title": "title"
    }
]


来自../sender/1的JSON:

[
    {
        "id": 1,
        "id_sender": 1,
        "id_recipient": 2,
        "data_time": "2018-12-06T16:41:30.000Z",
        "amount_money": 100,
        "transfer_title": "title pay"
    },
    {
        "id": 2,
        "id_sender": 1,
        "id_recipient": 3,
        "data_time": "2018-12-08T03:48:30.000Z",
        "amount_money": 20,
        "transfer_title": "title pay"
    }
]

最佳答案

尝试使用传播算子

即(用您从ajax请求中获得的任何内容替换它)

let data1 = [
    {
        "id": 1,
        "id_sender": 1,
        "id_recipient": 2,
        "data_time": "2018-12-06T16:41:30.000Z",
        "amount_money": 100,
        "transfer_title": "title pay"
    },
    {
        "id": 2,
        "id_sender": 1,
        "id_recipient": 3,
        "data_time": "2018-12-08T03:48:30.000Z",
        "amount_money": 20,
        "transfer_title": "title pay"
    }
]




let data2 = [
    {
        "id": 3,
        "id_sender": 2,
        "id_recipient": 1,
        "data_time": "2018-12-08T03:49:00.000Z",
        "amount_money": 30,
        "transfer_title": "title"
    }
]


let combinedData = [...data1, ...data2]
然后创建一个函数来对数据进行排序

const sortingData = (data) => { 
 return data.sort((a, b) => {
 return Date.parse(b["data_time"]) - Date.parse(a["data_time"])
  }) 
}


然后将combinedData传递给上述函数,即sortingData(combinedData)

关于javascript - REST API(React.js)中两个json对象的付款历史,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53679096/

相关文章:

javascript - OpenLayer 3 中层的概念究竟是如何使用的?

javascript - 如何只影响嵌套对象(React)中的一个键?

java - 如何在 linux 上从 rest 服务执行 sh 文件

api - RESTful API 安全性

javascript - 表中的 Angular ng-repeat,添加额外的行

javascript - 将函数分配给使用 PHP 脚本创建的选择

javascript - 如何在 angularjs 中将 $scope 变量与文本编辑器绑定(bind)

node.js - Next.js - 图像不是 'defined"

javascript - react Redux : How to add a property to an empty object in the reducer

javascript - 使用 jQuery 和 REST 创建 Amazon S3 存储桶