将对象内部的数组分隔为单个对象

时间:2019-05-16 18:22:58

标签: javascript node.js forms post

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

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

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

enter image description here

{ 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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

  

...或者更好的是如何将对象分离的表单发送出去。

不是将值作为数组推送,而是为每行使用一个单独的对象,然后将其推送到这样的数组中:

let row1= {
a: 1,
b: 2,
}

let row2 = {
a: 3,
b: 4,
}

let data = [row1, row2];