bugfix> javascript > 投稿

JSON配列を変換する方法

[
   {
      "travelExpenseId":11,
      "tripId":2,
      "paymentPurpose":"some payment purpose 2",
      "receiptNumber":"EF12312_2",
      "receiptDate":"2018-09-30T00:00:00",
      "receiptPrice":107000.0,
      "receiptCurrency":"руб."
   },
   {
      "travelExpenseId":10,
      "tripId":2,
      "paymentPurpose":"some payment purpose 1",
      "receiptNumber":"EF12312_1",
      "receiptDate":"2018-09-30T00:00:00",
      "receiptPrice":107000.0,
      "receiptCurrency":"руб."
   }
]

travelExpenseIdで費用をすばやく取得し、tripIdで費用を取得するのに適したものに。

今、私はこのようなものを使用します:

export const expenseSchema = new schema.Entity('expenses', {}, { idAttribute: 'travelExpenseId' });
export const expensesListSchema = [expenseSchema];

そして結果を得る:

data: {
  entities: {
    expenses: {
      '10': {
        travelExpenseId: 10,
        tripId: 2,
        paymentPurpose: 'some payment purpose 1',
        receiptNumber: 'EF12312_1',
        receiptDate: '2018-09-30T00:00:00',
        receiptPrice: 107000,
        receiptCurrency: 'руб.'
      },
      '11': {
        travelExpenseId: 11,
        tripId: 2,
        paymentPurpose: 'some payment purpose 2',
        receiptNumber: 'EF12312_2',
        receiptDate: '2018-09-30T00:00:00',
        receiptPrice: 107000,
        receiptCurrency: 'руб.'
      }
    }
  },
  result: [
    11,
    10
  ]
}

追加のスキーマ expensesByTripId を追加したいdata.entitiesに対して、結果は次のようになります。

data: {
  entities: {
    expensesByTripId: {
       '2': [10, 11],
    },
    expenses: {
      '10': {
        travelExpenseId: 10,
        tripId: 2,
        paymentPurpose: 'some payment purpose 1',
        receiptNumber: 'EF12312_1',
        receiptDate: '2018-09-30T00:00:00',
        receiptPrice: 107000,
        receiptCurrency: 'руб.'
      },
      '11': {
        travelExpenseId: 11,
        tripId: 2,
        paymentPurpose: 'some payment purpose 2',
        receiptNumber: 'EF12312_2',
        receiptDate: '2018-09-30T00:00:00',
        receiptPrice: 107000,
        receiptCurrency: 'руб.'
      }
    }
  },
  result: [
    11,
    10
  ]
}

または、私の場合に適した他の方法を提供してください

回答 1 件
  • let data = {
          entities: {
            expenses: {
              '10': {
                travelExpenseId: 10,
                tripId: 2,
                paymentPurpose: 'some payment purpose 1',
                receiptNumber: 'EF12312_1',
                receiptDate: '2018-09-30T00:00:00',
                receiptPrice: 107000,
                receiptCurrency: 'руб.'
              },
              '11': {
                travelExpenseId: 11,
                tripId: 2,
                paymentPurpose: 'some payment purpose 2',
                receiptNumber: 'EF12312_2',
                receiptDate: '2018-09-30T00:00:00',
                receiptPrice: 107000,
                receiptCurrency: 'руб.'
              }
            }
          },
          result: [
            11,
            10
          ]
        }
        let object = {};
        Object.values(data.entities.expenses).forEach(expenses => {
          
          let result = object[expenses.tripId] || [];
          object[expenses.tripId] = [...result, expenses.travelExpenseId];
        });
    		data.entities["expensesByTripId"] = object;
        console.log(data);
       
    
    

    どのように Normalizr がわからない  動作しますが、私はVanillaJSを使用して要件に応じてカスタムメイドします

あなたの答え