首页 > 解决方案 > 将对象内的数组分离为单个对象

问题描述

我正在尝试从对象内的数组创建 2 个不同的对象。

我制作了一个表格,通过 POST 方法传递所有数据。

例如,这是当我将数据添加到 2 行时表单发送的内容:

在此处输入图像描述

{ fm_tipo_comp_select: [ '1', '3' ],
  fm_tipo_gasto: [ '1', '6' ],
  fm_serie: [ '1234', '4321' ],
  fm_ndoc: [ '11551151', '222111' ],
  fm_ruc: [ '74111', '17144151658' ],
  fm_fecha: [ 'May 16, 2019', 'May 25, 2019' ],
  fm_moneda: [ '2', '5' ],
  fm_retencion: [ '16', '16' ] }

我需要像这样分开它:

对象 1

{ fm_tipo_comp_select: '1',
  fm_tipo_gasto: '1',
  fm_serie: '1234',
  fm_ndoc: '11551151',
  fm_ruc: '74111',
  fm_fecha: 'May 16, 2019',
  fm_moneda: '2',
  fm_retencion: '16' }

对象 2

{ fm_tipo_comp_select: '3',
      fm_tipo_gasto: '6',
      fm_serie: '4321',
      fm_ndoc: '222111',
      fm_ruc: '17144151658',
      fm_fecha: 'May 25, 2019',
      fm_moneda: '5',
      fm_retencion: '16' }

我怎样才能像那样分离我的对象,或者更好地如何发送一个分离对象的表格。

我正在使用nodejs。

- - 编辑 - -

这是我的表格:

form#fm-form.col.s12(method='POST', action='/facturas/m/add')
                .row
                    a.btn-floating.btn-small.waves-effect.waves-light.green.add-btn
                        i.material-icons add
                    table#fm-table
                        thead
                            tr
                                th Tipo de Comprobante
                                th Tipo de Gasto
                                th Serie
                                th N° Documento
                                th RUC
                                th Razón Social
                                th Fecha
                                th Moneda
                                th Monto
                                th Cod. Retención
                                th Eliminar

                        tbody
                            tr
                                td 
                                    .inputfield
                                        select(name='fm_tipo_comp_select')
                                            option(value='', disabled='', selected='') Comprobante
                                            each row in tipo_comprobante
                                                option(value=row.tipo_comprobante_id) #{row.tipo_comprobante_name}    
                                        label Tipo comprobante
                                td 
                                    .inputfield
                                        select(name='fm_tipo_gasto')
                                            option(value='', disabled='', selected='') Tipo gasto
                                            each row in tipo_gasto
                                                option(value=row.tipo_gasto_id) #{row.tipo_gasto_name}
                                        label Tipo gasto
                                td 
                                    .inputfield
                                        input#fm_serie.validate.right-align(type='text', name='fm_serie')
                                td 
                                    .inputfield
                                        input#fm_ndoc.validate.right-align(type='text', name='fm_ndoc')
                                td 
                                    .inputfield
                                        input#fm_ruc.validate.right-align(type='text', name='fm_ruc')
                                td 
                                    .inputfield
                                        input#fm_rs.validate.right-align(type='text', name='fm_rs'  disabled)
                                td 
                                    .inputfield
                                        input#fm_fecha.datepicker.validate(type='text', name='fm_fecha')
                                td 
                                    .inputfield
                                        select(name='fm_moneda')
                                            option(value='', disabled='', selected='') Moneda
                                            each row in currency
                                                option(value=row.currency_id) #{row.currency_name}
                                        label Moneda
                                td 
                                    .inputfield
                                        input#fm_monto.validate(type='number')
                                td 
                                    .inputfield
                                        select(name='fm_retencion')
                                            option(value='', disabled='', selected='') Retención
                                            each row in rendiciones
                                                option(value=row.rendicion_id) #{row.rendicion_name}
                                        label Retención
                                td
                                    .inputfield
                                        a.btn-floating.btn-small.waves-effect.waves-light.red.delete-btn
                                            i.material-icons delete

                .row.fm_buttons_row
                    .input-field.col.s4
                        button.btn.waves-effect.waves-light.red(type='submit', name='action')
                            | Cancelar
                            i.material-icons.right cancel
                    .input-field.col.s4
                        button.btn.waves-effect.waves-light(type='submit', name='action')
                            | Guardar
                            i.material-icons.right save
                    .input-field.col.s4
                        button.btn.waves-effect.waves-light(type='submit', name='action')
                            | Enviar
                            i.material-icons.right send

标签: javascriptnode.jsformspost

解决方案


您可以为每个项目获取一个自己的结果集对象。

var data = { fm_tipo_comp_select: ['1', '3'], fm_tipo_gasto: ['1', '6'], fm_serie: ['1234', '4321'], fm_ndoc: ['11551151', '222111'], fm_ruc: ['74111', '17144151658'], fm_fecha: ['May 16, 2019', 'May 25, 2019'], fm_moneda: ['2', '5'], fm_retencion: ['16', '16'] },
    result = Object
        .entries(data)
        .reduce((r, [k, a]) => a.map((v, i) => Object.assign({}, r[i], { [k]: v })), []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }


推荐阅读