简体   繁体   English

JavaScript转换数组2D中的对象数组

[英]JavaScript convert array of objects in array 2D

I need the following JS structure (results from request-promise query) 我需要以下JS结构(来自请求承诺查询的结果)

[ { idfactura: 2,
    idcuenta: 1,
    nombre: 'Nick',
    periodo: 'Per1                                          ',
    formapago: 'Tarj   ',
    cantidadpersonas: 1,
    subtotal: 7000,
    porcentajedescuento: 0,
    fecha: '24/11/2017 02:38' },
  { idfactura: 3,
    idcuenta: 1,
    nombre: 'Adm',
    periodo: 'Per1                                          ',
    formapago: 'Efec  ',
    cantidadpersonas: 1,
    subtotal: 7000,
    porcentajedescuento: 10,
    fecha: '25/11/2017 23:45' } ]

To the exact following structure (to export in xlsx): 完全遵循以下结构(以xlsx格式导出):

[[2, 1, 'Nick', 'Per1', 'Tarj', 1, 7000, 0, '24/11/2017 02:38'],
 [3, 1, 'Adm', 'Per1', 'Efec', 1, 7000, 10, '25/11/2017 23:45']]

I've tried with _ method, values, JSON.stringify as other posts in Stackoverflow but I am not able to get my exact output struct. 我已经尝试使用_方法,值,JSON.stringify作为Stackoverflow中的其他帖子,但是我无法获得确切的输出结构。

Example Code: 示例代码:

results = [ { idfactura: 2,
    idcuenta: 1,
    nombre: 'Nick',
    periodo: 'Per1                                          ',
    formapago: 'Tarj   ',
    cantidadpersonas: 1,
    subtotal: 7000,
    porcentajedescuento: 0,
    fecha: '24/11/2017 02:38' },
  { idfactura: 3,
    idcuenta: 1,
    nombre: 'Adm',
    periodo: 'Per1                                          ',
    formapago: 'Efec  ',
    cantidadpersonas: 1,
    subtotal: 7000,
    porcentajedescuento: 10,
    fecha: '25/11/2017 23:45' } ]

var arr = Object.key(results).map(function(key){
    return [results[key]];
});

You need to get the values from each object. 您需要从每个对象获取值。 While there are many ways, one way is where you can get the keys using Object.keys and map the values respectively using map function. 尽管有很多方法,但是一种方法是可以使用Object.keys获取键并使用map函数分别map值。

In ES7 you can just get the values of object using Object.values ES7您可以使用Object.values获取对象的值

Also since your array seem to have un-necessary spaces, you need to map them accordingly by trimming the spaces. 另外,由于数组似乎有不必要的空格,因此需要通过修剪空格来相应地映射它们。

 results = [ { idfactura: 2, idcuenta: 1, nombre: 'Nick', periodo: 'Per1 ', formapago: 'Tarj ', cantidadpersonas: 1, subtotal: 7000, porcentajedescuento: 0, fecha: '24/11/2017 02:38' }, { idfactura: 3, idcuenta: 1, nombre: 'Adm', periodo: 'Per1 ', formapago: 'Efec ', cantidadpersonas: 1, subtotal: 7000, porcentajedescuento: 10, fecha: '25/11/2017 23:45' } ] const mappedResults = results.map(result => Object.keys(result).map(key => typeof(result[key]) === "string" ? result[key].trim() : result[key] ) ) console.log(mappedResults) const mappedResultsES7 = results.map(result => Object.values(result).map(value => typeof(value) === "string" ? value.trim() : value ) ) console.log(mappedResultsES7) 

Create an array of keys in the order that you want, then use 2 nested Array#map calls to iterate the array, and then to iterate the keys and extract the data: 按照所需顺序创建键数组,然后使用2个嵌套的Array#map调用来迭代该数组,然后迭代键并提取数据:

 var oredredKeys = ['idfactura', 'idcuenta', 'nombre', 'periodo', 'formapago', 'cantidadpersonas', 'subtotal', 'porcentajedescuento', 'fecha']; var data = [{"idfactura":2,"idcuenta":1,"nombre":"Nick","periodo":"Per1","formapago":"Tarj ","cantidadpersonas":1,"subtotal":7000,"porcentajedescuento":0,"fecha":"24/11/2017 02:38"},{"idfactura":3,"idcuenta":1,"nombre":"Adm","periodo":"Per1","formapago":"Efec ","cantidadpersonas":1,"subtotal":7000,"porcentajedescuento":10,"fecha":"25/11/2017 23:45"}]; var result = data.map(function(o) { return oredredKeys.map(function(key) { return o[key]; }); }); console.log(result); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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