2016-06-16 121 views
1

比方说,我从服务器获取对象的有没有什么办法在JavaScript中将大对象转换为小对象?

`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" 
    }, 
     } 

是否有任何的JavaScript快速的方法来创建这个基础上选择的特性另一个对象?

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

不读这个:我被要求添加一些细节,但我想这已经足够了。

回答

1

我建议存储想要的性能

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

的路径,并与Array#reduce使用它的价值。

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

试试这个

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

谢谢,我加上一个从@NinaScholz你的答案! – boooni

相关问题