Let's say i get from the server the object A
假设我从服务器获取对象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"
},
}
Is there any fast way in JavaScript to create another object from this one based on selected properties?
在JavaScript中是否有任何基于所选属性的快速方法来创建另一个对象?
B = {"id": "8Q1wW6Us-O0C",
"title": "Years with Frank Lloyd Wright",
"publishedDate": "1979",
"pageCount": 228,
and some other properties}
Don't read this: I am asked to add some details, but I guess this is enough.
不读这个:我被要求添加一些细节,但我想这就足够了。
2 个解决方案
#1
1
I suggest to store the path of the wanted properties
我建议存储所需属性的路径
wanted = { id: 'id', title: 'volumeInfo.title', publishedDate: 'volumeInfo.publishedDate', pageCount: 'volumeInfo.pageCount' }
and use it with Array#reduce
for the value.
并使用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);
#2
1
try this
var selectedProperties = ["id", "title", "publishedDate", "pageCount"];
var B = {};
selectedProperties.forEach( function(key){
A[key] && (B[key] = A[key]);
});
#1
1
I suggest to store the path of the wanted properties
我建议存储所需属性的路径
wanted = { id: 'id', title: 'volumeInfo.title', publishedDate: 'volumeInfo.publishedDate', pageCount: 'volumeInfo.pageCount' }
and use it with Array#reduce
for the value.
并使用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);
#2
1
try this
var selectedProperties = ["id", "title", "publishedDate", "pageCount"];
var B = {};
selectedProperties.forEach( function(key){
A[key] && (B[key] = A[key]);
});