2017-12-02 240 views
0

我需要以下JS結構(結果從請求承諾查詢)的JavaScript轉換對象的陣列中的陣列2D

[ { 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']] 

我試過_方法,值,JSON.stringify作爲其他帖子在Stackoverflow,但我無法得到我確切的輸出結構。

例如代碼:

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

回答

1

你需要從每個對象獲得的值。雖然有很多方法,但有一種方法是使用Object.keys獲取密鑰,並分別使用map函數映射值。

ES7您可以使用Object.values

此外,由於你的陣列似乎有非必要的空間只有獲得對象的值,則需要通過調整空間相應映射。

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)

+0

您的片段並不在我的linux的NodeJS終端執行(--version的NodeJS v4.8.2):'''常量mappedResults = ... results.map(結果= (key)> ... Object.keys(result).map(key => ..... typeof(result [key])===「string」?result [key] .trim():result [key] SyntaxError:missing) – negrotico19

+0

您使用的是哪個節點?複製時缺少某些內容,因爲這適用於瀏覽器和代碼片段。 –

+0

(版本4.8.2) – negrotico19

0

在所需的訂單創建鍵陣列,然後用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);