簡體   English   中英

在 javascript 中轉換數組的最佳方法

[英]Best way to convert array in javascript

我正在開發一個反應應用程序,我從 api 調用中獲取以下數組。

var arrayLike ={ 
         "2020-W1": [
         { 
            "type": "TAX_REFUND_IN_INT", 
            "net_amount_base": "500001"
         },
         { 
            "type": "TAX_REFUNDIN_IN_EXT", 
            "net_amount_base": "500002"
         }
      ],
      "2020-W2": [
         { 
            "type": "RENTAL_LEASING_INCOME_IN_INT", 
            "net_amount_base": "5000"
         },
         { 
            "type": "DIVIDENTS_INCOME_IN_INT", 
            "net_amount_base": "15000"
         },
         { 
            "type": "LICENCE_INCOME_IN_EXT", 
            "net_amount_base": "10000"
         },
         { 
            "type": "OTHER_INCOME_IN_EXT", 
            "net_amount_base": "1000"
         } 
      ] 
   } 

我想像下面這樣轉換這個數組:

var new_arr =  var new_arr =  [{
            year_week: '2020-W1',
            TAX_REFUND_IN_INT: '1000',
            TAX_REFUNDIN_IN_EXT: '300' 
          },
          {
            year_week: '2020-W2',
            RENTAL_LEASING_INCOME_IN_INT: '2000',
            DIVIDENTS_INCOME_IN_INT: '15000',
            LICENCE_INCOME_IN_EXT: '10000',
            OTHER_INCOME_IN_EXT: '1000' 
          } ]

有人可以幫我怎么做嗎? 如果需要,我也可以更改源數組,因為我可以控制 API。如果需要更多信息,請告訴我。

您可以只遍歷數組,然后取出您想要的部分。 如果您的傳入數據在actual_arr

var actual_arr = {
  "2020-W1" :{
  0: {site_id: "004", year_week: "2020-W1", type: "RENTAL_LEASING_INCOME", in_out: "IN", int_ext: "INT","amount" :1000},
  1: {site_id: "004", year_week: "2020-W1", type: "DIVIDENTS_INCOME", in_out: "IN", int_ext: "INT","amount" :300},
  2: {site_id: "004", year_week: "2020-W1", type: "LICENCE_INCOME", in_out: "IN", int_ext: "INT","amount" :20},
  3: {site_id: "004", year_week: "2020-W1", type: "OTHER_INCOME", in_out: "IN", int_ext: "INT","amount" :100 },
  },
 }



const new_arr = Object.keys(actual_arr).map(key=> {
  // Build an object to receive the values
  const object = {year_week: key}
   Object.keys(actual_arr[key]).forEach(ix => {
     const income_name = actual_arr[key][ix].type
     const income_amount = actual_arr[key][ix].amount
      object[income_name] = income_amount
  })
  return object
})

 var arrayLike = { "2020-W1": [{ "type": "TAX_REFUND_IN_INT", "net_amount_base": "500001" }, { "type": "TAX_REFUNDIN_IN_EXT", "net_amount_base": "500002" } ], "2020-W2": [{ "type": "RENTAL_LEASING_INCOME_IN_INT", "net_amount_base": "5000" }, { "type": "DIVIDENTS_INCOME_IN_INT", "net_amount_base": "15000" }, { "type": "LICENCE_INCOME_IN_EXT", "net_amount_base": "10000" }, { "type": "OTHER_INCOME_IN_EXT", "net_amount_base": "1000" } ] } let newArray = [] Object.values(arrayLike).forEach((arr, index) => { let newObj = {} newObj['yearWeek'] = Object.keys(arrayLike)[index] arr.forEach((item) => { newObj[`${item.type}`] = item.net_amount_base }) newArray.push(newObj) }) console.log(newArray)

編輯:更新了一個片段!

您可以嘗試使用map進行一些數組操作, reduce

下面的片段可能會有所幫助

 const arr = { "2020-W1": { 0: { site_id: "004", year_week: "2020-W1", type: "RENTAL_LEASING_INCOME", in_out: "IN", int_ext: "INT", amount: 1000 }, 1: { site_id: "004", year_week: "2020-W1", type: "DIVIDENTS_INCOME", in_out: "IN", int_ext: "INT", amount: 300 }, 2: { site_id: "004", year_week: "2020-W1", type: "LICENCE_INCOME", in_out: "IN", int_ext: "INT", amount: 20 }, 3: { site_id: "004", year_week: "2020-W1", type: "OTHER_INCOME", in_out: "IN", int_ext: "INT", amount: 100 }, }, "2020-W2": { 0: { site_id: "004", year_week: "2020-W2", type: "RENTAL_LEASING_INCOME", in_out: "IN", int_ext: "INT", amount: 2000 }, 1: { site_id: "004", year_week: "2020-W2", type: "DIVIDENTS_INCOME", in_out: "IN", int_ext: "INT", amount: 400 }, 2: { site_id: "004", year_week: "2020-W2", type: "LICENCE_INCOME", in_out: "IN", int_ext: "INT", amount: 2000 }, 3: { site_id: "004", year_week: "2020-W2", type: "OTHER_INCOME", in_out: "IN", int_ext: "INT", amount: 5000 }, }, "2020-W3": { 0: { site_id: "004", year_week: "2020-W3", type: "RENTAL_LEASING_INCOME", in_out: "IN", int_ext: "INT", amount: 1000 }, 1: { site_id: "004", year_week: "2020-W3", type: "DIVIDENTS_INCOME", in_out: "IN", int_ext: "INT", amount: 2000 }, 2: { site_id: "004", year_week: "2020-W3", type: "LICENCE_INCOME", in_out: "IN", int_ext: "INT", amount: 3000 }, 3: { site_id: "004", year_week: "2020-W3", type: "OTHER_INCOME", in_out: "IN", int_ext: "INT", amount: 4000 }, }, } const res = Object.keys(arr).map((year_week) => { const typeAmounts = Object.keys(arr[year_week]).reduce( (acc, index) => ({...acc, [arr[year_week][index].type]: arr[year_week][index].amount, }), {} ) return { year_week, ...typeAmounts, } }) console.log(res)

試試這個,這給出了您正在尋找的確切格式(對象數組)。

在這種情況下,我使用 Set() 刪除重復項,然后使用擴展運算符 (...) 將其轉換回包含所有唯一對象的數組。 也有一些方法可以用 .reduce() 和 .filter() 做同樣的事情。 以下是一些可能有用的資源。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/Set

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

https://www.w3schools.com/jsref/jsref_reduce.asp

https://www.w3schools.com/jsref/jsref_filter.asp

 let arrayLike = { "2020-W1": [{ "type": "TAX_REFUND_IN_INT", "net_amount_base": "500001" }, { "type": "TAX_REFUNDIN_IN_EXT", "net_amount_base": "500002" } ], "2020-W2": [{ "type": "RENTAL_LEASING_INCOME_IN_INT", "net_amount_base": "5000" }, { "type": "DIVIDENTS_INCOME_IN_INT", "net_amount_base": "15000" }, { "type": "LICENCE_INCOME_IN_EXT", "net_amount_base": "10000" }, { "type": "OTHER_INCOME_IN_EXT", "net_amount_base": "1000" } ] } function solveProblem(arrayLike) { let final_array = [] let newObjectOne = {} let newObjectTwo = {} for (i in arrayLike) { for (z in i) { if (i === "2020-W1") { if (arrayLike[i][z],== undefined) { newObjectOne["year_week"] = "2020-W1". newObjectOne[arrayLike[i][z].type] = arrayLike[i][z].net_amount_base final_array,push(newObjectOne) } } if (i === "2020-W2") { if (arrayLike[i][z].== undefined) { newObjectTwo["year_week"] = "2020-W2". newObjectTwo[arrayLike[i][z].type] = arrayLike[i][z].net_amount_base final_array.push(newObjectTwo) } } } } let unqiueItemsArr = new Set(final_array) return [...unqiueItemsArr] } //console.log(solveProblem(arrayLike)) to see new array of objects console.log(solveProblem(arrayLike))

使用Object.entreismapObject.fromEntries進行轉換。

 const convert = data => Object.entries(data).map(([year_week, arr]) => ({ year_week, ...Object.fromEntries(arr.map(Object.values)), })); var arrayLike = { "2020-W1": [ { type: "TAX_REFUND_IN_INT", net_amount_base: "500001", }, { type: "TAX_REFUNDIN_IN_EXT", net_amount_base: "500002", }, ], "2020-W2": [ { type: "RENTAL_LEASING_INCOME_IN_INT", net_amount_base: "5000", }, { type: "DIVIDENTS_INCOME_IN_INT", net_amount_base: "15000", }, { type: "LICENCE_INCOME_IN_EXT", net_amount_base: "10000", }, { type: "OTHER_INCOME_IN_EXT", net_amount_base: "1000", }, ], }; console.log(convert(arrayLike));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM