gpt4 book ai didi

javascript - lodash 在嵌套数组中选择

转载 作者:行者123 更新时间:2023-11-30 06:50:55 25 4
gpt4 key购买 nike

我有一个像这样的对象数组:

{
"sizes":{
"thumbnail":{
"height":300,
"width":300,
"url":"http://example.com/wp-content/uploads/2017/04/web-300x300.jpg",
"orientation":"landscape"
},
"medium":{
"height":267,
"width":400,
"url":"http://example.com/wp-content/uploads/2017/04/web-400x267.jpg",
"orientation":"landscape"
},
"large":{
"height":441,
"width":660,
"url":"http://example.com/wp-content/uploads/2017/04/web-1024x684.jpg",
"orientation":"landscape"
},
"full":{
"url":"http://example.com/wp-content/uploads/2017/04/web.jpg",
"height":1200,
"width":1796,
"orientation":"landscape"
}
},
"mime":"image/jpeg",
"type":"image",
"subtype":"jpeg",
"id":3589,
"url":"http://example.com/wp-content/uploads/2017/04/web.jpg",
"alt":"",
"link":"http://example.com/web/",
"caption":""
}

我使用以下代码片段创建一个新数组,其中仅包含 altcaptionidurl 数组中的键:

images.map( ( image ) => pick( image, [ 'alt', 'caption', 'id', 'url' ] ) ),

我的问题是,如何选择 sizes.thumbnail.url 键而不是根 url 键?是否可以?如果是这样,怎么办?

提前致谢

最佳答案

使用 _.get() 创建一个具有 url 属性和 sizes.thumbnail.url 值的对象,并将其与 _.pick() 的结果相结合。

注意:我使用过 object spread合并结果,但您可以使用 Object.assign()或 lodash 的等效项。

const images = [{"sizes":{"thumbnail":{"height":300,"width":300,"url":"http://example.com/wp-content/uploads/2017/04/web-300x300.jpg","orientation":"landscape"},"medium":{"height":267,"width":400,"url":"http://example.com/wp-content/uploads/2017/04/web-400x267.jpg","orientation":"landscape"},"large":{"height":441,"width":660,"url":"http://example.com/wp-content/uploads/2017/04/web-1024x684.jpg","orientation":"landscape"},"full":{"url":"http://example.com/wp-content/uploads/2017/04/web.jpg","height":1200,"width":1796,"orientation":"landscape"}},"mime":"image/jpeg","type":"image","subtype":"jpeg","id":3589,"url":"http://example.com/wp-content/uploads/2017/04/web.jpg","alt":"","link":"http://example.com/web/","caption":""}];

const result = images.map((image) => ({
..._.pick(image, ['alt', 'caption', 'id']),
url: _.get(image, 'sizes.thumbnail.url')
}));

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

一个更通用的解决方案是一个接受路径列表的函数,并生成一个数组对[路径的最后部分,值]。该函数使用 _.fromPairs() 将对转换为对象。 (或Object.fromEntries()):

const deepPick = (paths, obj) => 
_.fromPairs(paths.map(p => [
_.last(p.split('.')),
_.get(obj, p),
]))

const images = [{"sizes":{"thumbnail":{"height":300,"width":300,"url":"http://example.com/wp-content/uploads/2017/04/web-300x300.jpg","orientation":"landscape"},"medium":{"height":267,"width":400,"url":"http://example.com/wp-content/uploads/2017/04/web-400x267.jpg","orientation":"landscape"},"large":{"height":441,"width":660,"url":"http://example.com/wp-content/uploads/2017/04/web-1024x684.jpg","orientation":"landscape"},"full":{"url":"http://example.com/wp-content/uploads/2017/04/web.jpg","height":1200,"width":1796,"orientation":"landscape"}},"mime":"image/jpeg","type":"image","subtype":"jpeg","id":3589,"url":"http://example.com/wp-content/uploads/2017/04/web.jpg","alt":"","link":"http://example.com/web/","caption":""}];

const result = images.map(image => deepPick(
['alt', 'caption', 'id', 'sizes.thumbnail.url'],
image
));

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

关于javascript - lodash 在嵌套数组中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50096091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com