JavaScript转换数组2D中的对象数组
我需要以下来自请求承诺查询的JS结构结果JavaScript转换数组2D中的对象数组,javascript,arrays,json,object,request-promise,Javascript,Arrays,Json,Object,Request Promise,我需要以下来自请求承诺查询的JS结构结果 [ { idfactura: 2, idcuenta: 1, nombre: 'Nick', periodo: 'Per1 ', formapago: 'Tarj ', cantidadpersonas: 1, subtotal: 7000, porcentajedescuento: 0, fecha: '
[ { 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' } ]
要导出到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']]
我在Stackoverflow中尝试了uMethod、values、JSON.stringify和其他帖子,但我无法获得确切的输出结构
示例代码:
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]];
});
按所需顺序创建键数组,然后使用2个嵌套调用迭代该数组,然后迭代键并提取数据: var oredredKeys=['idfactura'、'idcuenta'、'nombre'、'periodo'、'formapago'、'cantidadpersonas'、'小计'、'Porcentajedescunto'、'fecha']; var数据=[{idfactura:2,idcuenta:1,nombre:Nick,periodo:Per1,formapago:Tarj,cantidadpersonas:1,小计:7000,Porcentajedescunto:0,fecha:24/11/2017 02:38},{idfactura:3,idcuenta:1,nombre:Adm,periodo:Per1,formapago:Efec,cantidadpersonas:1,小计:7000,Porcentajedescunto:10,fecha:25/11/2017 23:45}]; var result=data.mapfunctiono{ 返回oredredKeys.mapfunctionkey{ 返回o[键]; }; };
console.logresult 您需要从每个对象获取值。虽然有很多方法,但其中一种方法是使用Object.keys获取关键点,并使用map函数分别映射值 在ES7中,您可以使用object.values获取object的值 此外,由于数组似乎有不必要的空格,因此需要通过修剪空格来相应地映射它们 结果=[{idfactura:2, idcuenta:1, 名字:“尼克”, periodo:‘Per1’, 福尔马帕戈:“Tarj”, cantidadpersonas:1, 小计:7000, Porcentajedescento:0, fecha:'24/11/2017 02:38'}, {以色列国防军:3, idcuenta:1, 名称:“Adm”, periodo:‘Per1’, 福尔马帕戈:“Efec”, cantidadpersonas:1, 小计:7000, 波森塔耶德斯库托:10, fecha:'25/11/2017 23:45'}] 常量mappedResults= results.mapreult=> Object.keysresult.mapkey=> typeofresult[键]==字符串?结果[键]。修剪:结果[键] console.logmappedResults 常量MappedResultse7= results.mapreult=> Object.valuesresult.mapvalue=> typeofvalue===字符串?value.trim:value
console.LogMappedResultses7您的代码段不会在my nodejs linux终端nodejs-v4.8.2版中执行。示例:const mappedResults=。。。results.mapreult=>。。。Object.keysresult.mapkey=>。。。。。typeofresult[键]==字符串?结果[key]。修剪:结果[key]语法错误:参数列表后缺少您使用的节点?您在复制时丢失了一些内容,因为这在浏览器和代码段上都有效。版本4.8.2