假设我从服务器获取对象A
`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}
请勿阅读:我被要求添加一些细节,但我想这已经足够了。
答案 0 :(得分:1)
试试这个
var selectedProperties = ["id", "title", "publishedDate", "pageCount"];
var B = {};
selectedProperties.forEach( function(key){
A[key] && (B[key] = A[key]);
});
答案 1 :(得分: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);