2017-12-19 1 views
0

Ich habe ein json Array wie folgt, diese Daten sind von einem Excel-Blatt und konvertiert es in json Format.Dynamische JSON-Array machen denselben Schlüssel wie ein Array

[{ 
    'booktitle': 'Leading', 
    'bookid': '56353', 
    'bookauthor': 'Sir Alex Ferguson' 
}, { 
    'booktitle': 'How Google Works', 
    'bookid': '73638', 
    'bookauthor': 'Eric Smith' 
}, { 
    'booktitle': 'The Merchant of Venice', 
    'bookid': '37364', 
    'bookauthor': 'William Shakespeare' 
}] 

Ich möchte Wert mit demselben Schlüssel in einem Array erhalten. Dieses Schlüsselwertpaar wird dynamisch sein, also im Allgemeinen eine Lösung wünschen.

+0

Was hier der Schlüssel ist, kann erklären Sie etwas mehr deutlich –

+1

Was hast du probiert? Veröffentlichen Sie Ihren Versuch. – vibhor1997a

+0

[Was ist der Unterschied zwischen JSON und Object Literal Notation?] (Https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – Andreas

Antwort

0

Sie können wie folgt dies mit Array-Karte Objektschlüssel tun,

let arr = [{ 
 
    "booktitle": "Leading", 
 
    "bookid": "56353", 
 
    "bookauthor": "Sir Alex Ferguson" 
 
}, { 
 
    "booktitle": "How Google Works", 
 
    "bookid": "73638", 
 
    "bookauthor": "Eric Smith" 
 
}, { 
 
    "booktitle": "The Merchant of Venice", 
 
    "bookid": "37364", 
 
    "bookauthor": "William Shakespeare" 
 
}]; 
 

 
let ans=Object.keys(arr[0]).map((key) => { 
 
    let o={}; 
 
    o[key]=arr.map((x) => x[key]); 
 
    return o; 
 
}); 
 

 
console.log(ans);

+0

Dieser Code half. Nur benötigt, um zu lassen, ließ Daten = JSON.parse (arr), da mein json Feld Formatierung benötigt. Danke – Dev

1

Ich denke, was Sie suchen, ist dies. Wenn Sie etwas anderes hinzufügen möchten, lassen Sie es mich wissen. Überprüfen Sie das Code-Snippet.

const obj = [{ 
 
    'booktitle': 'Leading', 
 
    'bookid': '56353', 
 
    'bookauthor': 'Sir Alex Ferguson' 
 
}, { 
 
    'booktitle': 'How Google Works', 
 
    'bookid': '73638', 
 
    'bookauthor': 'Eric Smith' 
 
}, { 
 
    'booktitle': 'The Merchant of Venice', 
 
    'bookid': '37364', 
 
    'bookauthor': 'William Shakespeare' 
 
}]; 
 

 
const res = obj.reduce((a, b) => { 
 
    for(let i in b) { 
 
    if(!a[i]) { 
 
     a[i] = []; 
 
    } 
 
    a[i].push(b[i]); 
 
    } 
 
    
 
    return a; 
 
}, {}); 
 

 
console.log(res);

0
let bookInfo = [{ 
    'booktitle': 'Leading', 
    'bookid': '56353', 
    'bookauthor': 'Sir Alex Ferguson' 
}, { 
    'booktitle': 'How Google Works', 
    'bookid': '73638', 
    'bookauthor': 'Eric Smith' 
}, { 
'booktitle': 'The Merchant of Venice', 
'bookid': '37364', 
'bookauthor': 'William Shakespeare' 
}] 

    var bookTitle = []; 
    var bookId = []; 
    var bookAuthor = []; 

bookInfo.filter(function(data){ 
     if(data.booktitle){ 
      bookTitle.push(data.booktitle); 
     } 

     if(data.bookid){ 
      bookId.push(data.bookid); 
     } 


     if(data.bookauthor){ 
      bookAuthor.push(data.bookauthor); 
     } 

}) 
Verwandte Themen