简体   繁体   English

如何使用 js 数组方法(例如 map()、reduce()、filter() 等格式化来自服务器的响应,如下所示?

[英]How can I format the response coming from the server as given below using js array methods such as map(), reduce(), filter() and etc?

This is the response object from an API that I need to reformat:这是来自我需要重新格式化的 API 的响应对象:

const data = {
  id: 12,
  currency: "USD",
  transactions: [
    {
      from_wallet_id: 14,
      transaction_amount: '30.00',
      type: 'income',
      "date": "2019-12-04T06:45:49.394000+06:00"
    },
    {
      from_wallet_id: 11,
      transaction_amount: '50.00',
      type: 'expenses',
      date: "2019-12-04T06:45:49.394000+06:00"
    },
    {
      from_wallet_id: 11,
      transaction_amount: '70.00',
      type: 'transfer',
      date: "2019-12-06T06:45:49.394000+06:00"
    },
    {
      from_wallet_id: 14,
      transaction_amount: '40.00',
      type: 'transfer',
      date: "2019-12-08T06:45:49.394000+06:00"
    },
  ]
}

I need to change it to the example given below (using array methods such as reduce, map, filter, etc) A little bit information: income is sum of transactions at that date.我需要将其更改为下面给出的示例(使用reduce、map、filter 等数组方法) 一点信息:收入是该日期的交易总和。 expenses is sum of transactions at that date.费用是当日交易的总和。 type "transfer" can be income or expense If type is "transfer" and from_wallet_id is not equals to data.id then it will be added to income otherwise it will be added to expenses type "transfer" 可以是收入或费用 如果 type 是 "transfer" 并且 from_wallet_id 不等于 data.id 那么它将被添加到收入中否则它将被添加到费用中

formattedObject = {
  "2019-12-04": {
    date: "2019-12-04",
    income: 30,
    expenses: 50
  },
  "2019-12-06": {
    date: "2019-12-06",
    income: 0,
    expenses: 70
  },
  "2019-12-08": {
    date: "2019-12-08",
    income: 40,
    expenses: 0
  }
}

You could reduce the array nad take an adjustment for type: 'transfer' .您可以减少数组并调整type: 'transfer'

 const data = { id: 12, currency: "USD", transactions: [{ from_wallet_id: 14, transaction_amount: '30.00', type: 'income', date: "2019-12-04T06:45:49.394000+06:00" }, { from_wallet_id: 11, transaction_amount: '50.00', type: 'expenses', date: "2019-12-04T06:45:49.394000+06:00" }, { from_wallet_id: 11, transaction_amount: '70.00', type: 'transfer', date: "2019-12-06T06:45:49.394000+06:00" }, { from_wallet_id: 14, transaction_amount: '40.00', type: 'transfer', date: "2019-12-08T06:45:49.394000+06:00" }] }, result = data.transactions.reduce((r, { from_wallet_id, transaction_amount, type, date }) => { date = date.slice(0, 10); if (type === 'transfer') type = data.id === from_wallet_id ? 'expenses' : 'income'; r[date] = r[date] || { date, income: 0, expenses: 0 }; r[date][type] += +transaction_amount; return r; }, {}); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 React js 中使用 redux 来自 api 的 map 阵列响应。 - How to map array response coming from the api using redux in react js in react js.? 链接数组方法(过滤器、map、reduce)而不是使用双循环 - Chaining array methods (filter, map, reduce) instead of using double loop 使用[过滤器,映射,一些,化简等]从2d数组的子对象创建1d数组 - Create 1d array from 2d array's sub objects using [filter, map, some, reduce etc] 使用函数式编程在 Typescript 中过滤数组 [filter、map、some、reduce 等] - Filter array in Typescript using functional programing [filter, map, some, reduce etc] 我想从服务器获取文件,然后使用 react js 在浏览器中显示(文件作为响应来自服务器) - i want to fetch file from server and then display in browser using react js(file is coming from server as response) 如何排序 / map / 过滤以下 javascript 对象数组从一种格式到另一种格式 - How can I sort / map / filter the following javascript array of objects from one format to another javascript数组方法(map,forEach,reduce等)的迭代顺序是否确定? - Is the order of iteration for javascript array methods (map, forEach, reduce, etc) deterministic? JavaScript:如何将数组forEach,映射,过滤器等转换为“ for”循环? - JavaScript: How can i transpile array forEach, map, filter and etc to 'for' loop? 使用 map 从嵌套数组打印,在 javascript 中减少和过滤 - Print from nested array using map, reduce and filter in javascript 如何将来自服务器的日期格式化为客户端机器的日期格式? - How can I format the date coming from the server to date format of client machine?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM