简体   繁体   中英

Best way to convert array in javascript

I am developing a react app, and I am getting the below array from a api call.

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"
         } 
      ] 
   } 

I want to convert this array like below:

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' 
          } ]

Can someone please help me how to do this? I can change the source array also if required since I have control over the API.Please let me know if any more information is required.

You can just iterate over the array, and pull out the parts that you want. If your incoming data is in 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)

Edit: Updated with a snippet!

You could try some array manipulation with map , reduce .

Below snippet could help

 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)

Try this, this gives the exact format that you are looking for (an Array of Objects).

I used Set() in this case to remove duplicates and then used the spread operator (...) to turn it back into an array with all the unique objects. There are ways to do the same thing with.reduce() and.filter() as well. Here are some resources that may be helpful.

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))

Use Object.entreis , map , Object.fromEntries to convert.

 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM