2016-06-16 5 views
1

Sagen wir, ich vom Server AGibt es eine Möglichkeit, ein großes Objekt in JavaScript in ein kleiner zu konvertieren?

`A = { 
    "kind": "books#volume", 
    "id": "8Q1wW6Us-O0C", 
    "etag": "k2MS/7WPcsY", 
    "selfLink": "https://www.googleapis.com/books/v1/volumes/8Q1wW6Us-O0C", 
    "volumeInfo": { 
    "title": "Years with Frank Lloyd Wright", 
    "subtitle": "Apprentice to Genius", 
    "authors": [ 
     "Edgar Tafel" 
    ], 
    "publisher": "Courier Corporation", 
    "publishedDate": "1979", 
    "description": "This insightful memoir by a former apprentice presents a revealing portrait of the great American architect, providing illuminating anecdotes about Wright's Prairie home and Oak Park periods, and much more.", 
    "industryIdentifiers": [ 
     { 
     "type": "ISBN_10", 
     "identifier": "0486248011" 
     }, 
     { 
     "type": "ISBN_13", 
     "identifier": "9780486248011" 
     } 
    ], 
    "readingModes": { 
     "text": false, 
     "image": true 
    }, 
    "pageCount": 228, 
    "printType": "BOOK", 
    "categories": [ 
     "Architecture" 
    ], 
    "averageRating": 3.5, 
    "ratingsCount": 2, 
    "maturityRating": "NOT_MATURE", 
    "allowAnonLogging": false, 
    "contentVersion": "1.1.1.0.preview.1", 
    "imageLinks": { 
     "smallThumbnail": "http://books.google.ru/books/content?id=8Q1wW6Us-O0C&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api", 
     "thumbnail": "http://books.google.ru/books/content?id=8Q1wW6Us-O0C&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api" 
    }, 
    "previewLink": "http://books.google.ru/books?id=8Q1wW6Us-O0C&printsec=frontcover&hl=&source=gbs_api", 
    "infoLink": "http://books.google.ru/books?id=8Q1wW6Us-O0C&hl=&source=gbs_api", 
    "canonicalVolumeLink": "http://books.google.ru/books/about/Years_with_Frank_Lloyd_Wright.html?hl=&id=8Q1wW6Us-O0C" 
    }, 
     } 

Gibt es eine schnelle Möglichkeit, in JavaScript das Objekt bekommen ein anderes Objekt aus dieser einen auf ausgewählte Eigenschaften auf Basis zu schaffen?

B = {"id": "8Q1wW6Us-O0C", 
    "title": "Years with Frank Lloyd Wright", 
    "publishedDate": "1979", 
     "pageCount": 228, 
     and some other properties} 

Nicht lesen: Ich werde gebeten, einige Details hinzuzufügen, aber ich denke, das ist genug.

Antwort

1

Ich schlage vor, den Pfad der gewünschten Eigenschaften zu speichern

wanted = { 
    id: 'id', 
    title: 'volumeInfo.title', 
    publishedDate: 'volumeInfo.publishedDate', 
    pageCount: 'volumeInfo.pageCount' 
} 

und es mit Array#reduce für den Wert zu verwenden.

var data = { "kind": "books#volume", "id": "8Q1wW6Us-O0C", "etag": "k2MS/7WPcsY", "selfLink": "https://www.googleapis.com/books/v1/volumes/8Q1wW6Us-O0C", "volumeInfo": { "title": "Years with Frank Lloyd Wright", "subtitle": "Apprentice to Genius", "authors": ["Edgar Tafel"], "publisher": "Courier Corporation", "publishedDate": "1979", "description": "This insightful memoir by a former apprentice presents a revealing portrait of the great American architect, providing illuminating anecdotes about Wright's Prairie home and Oak Park periods, and much more.", "industryIdentifiers": [{ "type": "ISBN_10", "identifier": "0486248011" }, { "type": "ISBN_13", "identifier": "9780486248011" }], "readingModes": { "text": false, "image": true }, "pageCount": 228, "printType": "BOOK", "categories": ["Architecture"], "averageRating": 3.5, "ratingsCount": 2, "maturityRating": "NOT_MATURE", "allowAnonLogging": false, "contentVersion": "1.1.1.0.preview.1", "imageLinks": { "smallThumbnail": "http://books.google.ru/books/content?id=8Q1wW6Us-O0C&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api", "thumbnail": "http://books.google.ru/books/content?id=8Q1wW6Us-O0C&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api" }, "previewLink": "http://books.google.ru/books?id=8Q1wW6Us-O0C&printsec=frontcover&hl=&source=gbs_api", "infoLink": "http://books.google.ru/books?id=8Q1wW6Us-O0C&hl=&source=gbs_api", "canonicalVolumeLink": "http://books.google.ru/books/about/Years_with_Frank_Lloyd_Wright.html?hl=&id=8Q1wW6Us-O0C" } }, 
 
    wanted = { id: 'id', title: 'volumeInfo.title', publishedDate: 'volumeInfo.publishedDate', pageCount: 'volumeInfo.pageCount' }, 
 
    result = {}; 
 

 
Object.keys(wanted).forEach(function (k) { 
 
    result[k] = wanted[k].split('.').reduce(function (r, a) { 
 
     return r && r[a]; 
 
    }, data); 
 
}) 
 

 
console.log(result);

1

versuchen diese

var selectedProperties = ["id", "title", "publishedDate", "pageCount"]; 
var B = {}; 
selectedProperties.forEach(function(key){ 
    A[key] && (B[key] = A[key]); 
}); 
+0

Dank habe ich Ihre Antwort mit einem von @NinaScholz kombiniert! – boooni

Verwandte Themen