【发布时间】:2021-05-17 08:40:08
【问题描述】:
我正在尝试制作一个使用来自 flickr api 的数据的应用程序(我将只使用开放许可证图像并注明作者等)。我正在查询 flickr api,然后将结果解析为关于我正在搜索的每个项目的一行。基本上,我希望每一行都有我要搜索的东西的名称,然后是图片链接 + 图片作者,如下所示:
但是由于 api 结果不同(有时 flickr 在结果中给我 1 张图像,有时有数百张),我想不出一种优雅的方式将对象数组转换为一个对象。我基本上是在做从行到列的转置。因为数组长度不同,我最终使用了一堆可怕的if else 子句来完成这项工作。
// slicing the first 5 elements from flickr results
let first5elements = data.photos.photo.slice(0, 5);
let photos_with_query = [];
if (first5elements.length === 0) {
let onerowforecoregions = {
region_name: ecoregion.us_l4name,
image_1: first5elements[0].url_c,
image_author_1: first5elements[0].pathalias,
};
console.log(onerowforecoregions);
photos_with_query.push(onerowforecoregions);
} else if (first5elements.length === 1) {
let onerowforecoregions = {
region_name: ecoregion.us_l4name,
image_1: first5elements[0].url_c,
image_author_1: first5elements[0].pathalias,
};
console.log(onerowforecoregions);
photos_with_query.push(onerowforecoregions);
} else if (first5elements.length === 2) {
let onerowforecoregions = {
region_name: ecoregion.us_l4name,
image_1: first5elements[0].url_c,
image_author_1: first5elements[0].pathalias,
image_2: first5elements[1].url_c,
image_author_2: first5elements[1].pathalias,
};
console.log(onerowforecoregions);
photos_with_query.push(onerowforecoregions);
} else if (first5elements.length === 3) {
let onerowforecoregions = {
region_name: ecoregion.us_l4name,
image_1: first5elements[0].url_c,
image_author_1: first5elements[0].pathalias,
image_2: first5elements[1].url_c,
image_author_2: first5elements[1].pathalias,
image_3: first5elements[2].url_c,
image_author_3: first5elements[2].pathalias,
};
console.log(onerowforecoregions);
photos_with_query.push(onerowforecoregions);
} else if (first5elements.length === 4) {
let onerowforecoregions = {
region_name: ecoregion.us_l4name,
image_1: first5elements[0].url_c,
image_author_1: first5elements[0].pathalias,
image_2: first5elements[1].url_c,
image_author_2: first5elements[1].pathalias,
image_3: first5elements[2].url_c,
image_author_3: first5elements[2].pathalias,
image_4: first5elements[3].url_c,
image_author_4: first5elements[3].pathalias,
};
console.log(onerowforecoregions);
photos_with_query.push(onerowforecoregions);
} else if (first5elements.length === 5) {
let onerowforecoregions = {
region_name: ecoregion.us_l4name,
image_1: first5elements[0].url_c,
image_author_1: first5elements[0].pathalias,
image_2: first5elements[1].url_c,
image_author_2: first5elements[1].pathalias,
image_3: first5elements[2].url_c,
image_author_3: first5elements[2].pathalias,
image_4: first5elements[3].url_c,
image_author_4: first5elements[3].pathalias,
image_5: first5elements[4].url_c,
image_author_5: first5elements[4].pathalias,
};
photos_with_query.push(onerowforecoregions);
}
这确实有效......但它远非你所能得到的优雅。所以我想知道是否有一种方法可以获取一组对象并将其转换为一个具有原始对象属性的多个属性的对象。它会自动拉出 image_1、image_10 等的东西吗?
【问题讨论】:
-
一般来说,你不应该使用具有可数属性(image1,image2)的对象,而应该使用数组。但是如果你仍然想映射它,你可以遍历数组并将值分配给动态生成的对象键:
array.forEach((item, index) => object['image_' + index] = item);你也可以使用reduce()代替。
标签: javascript arrays sorting object ecmascript-6