Angualr -6/js中ngx数据表的嵌套json数据最小化 [英] nested json data minipulation for ngx datatable in angualr -6 / js
问题描述
我正在尝试创建一个ngx数据表,该数据表从嵌套数组动态创建列,但无法进行某些研究-因此,要达到我想要的结果,我必须使用每个数组所需的键/值对嵌套数组进行展平将对象嵌套到我的父对象中.
I am trying to create a ngx datatable that creates columns dynamically from nested arrays, which with some research is not possible - so to achieve my desired result, I must flatten my nested arrays with the key / values that i need from each nested object into my parent object.
我需要操纵数据,以便最终结果是一个平面数组,并为嵌套数组收益中的每个对象包含一个行项,其中'缩写'为键,而'金额" 是值.
I need to manipulate my data so that my end result is a flat array and contains a line item for each object in the nested array earnings with 'abbreviation' being the key and 'amount' being the value..
即
[
{
employee_uuid: 978f37df-7e07-4118-be93-d82507ce5c46,
employee_code: JB00024,
full_name: Thulisile Sandra,
last_name: Bhekiswayo
earnings:[{
abbreviation: "NT HRS"
amount: "45.00"
money: false
name: "Normal Time HRS"
time: true
unique: "7d783469-717e-408a-bc3c-93115cb632dd_true"
uuid: "7d783469-717e-408a-bc3c-93115cb632dd"
value: "45.00"
},
{
abbreviation: "OT HRS"
amount: "25.00"
money: false
name: "Normal Time HRS"
time: true
unique: "7d783469-717e-408a-bc3c-93115cb632dd_true"
uuid: "7d783469-717e-408a-bc3c-93115cb632dd"
value: "45.00"
}],
terminated false
}
...
]
我想看起来像这样:
[
{
employee_uuid: 978f37df-7e07-4118-be93-d82507ce5c46,
employee_code: JB00024,
full_name: Thulisile Sandra,
last_name: Bhekiswayo,
NT HRS: '45.00',
OT HRS, '25.00',
terminated:false
}
...
]
我不确定该怎么做,我已经尝试过简化和映射函数,但是没有成功..我可以使用Object.assign将嵌套数组添加到父对象中,但这需要整个对象,我需要从该对象创建一个新参数.
I am not sure how to go about this, I've tried reducing and map functions but no success.. I can add the nested arrays to the parent object with Object.assign but that takes the whole object, I need to create a new parameter from that object..
任何帮助将不胜感激.
推荐答案
您可以为此使用es6解构,只需公开所需的任何属性,然后重构"为所需的对象形状. 例如:
You can use es6 destructuring for this, simply expose whatever properties you need and then "recompose" then into the object shape you want. For example:
return myEmployeeArray.map(employee => {
const {earn } = employee
earn.map(field => field.abbreviation)
myDesiredObject = { fieldA: employee.abbreviation....fieldE:field.abbreviation}
}
这将使您成为嵌套字段之一
This would get you one of your nested fields
这篇关于Angualr -6/js中ngx数据表的嵌套json数据最小化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!