lee*_*mon 2 javascript arrays lodash
我有一个像这样的对象数组:
{
"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":""
}
Run Code Online (Sandbox Code Playgroud)
我使用以下代码片段创建一个新数组,其中仅包含数组中的alt、caption和键:idurl
images.map( ( image ) => pick( image, [ 'alt', 'caption', 'id', 'url' ] ) ),
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何选择sizes.thumbnail.url密钥而不是根url密钥?是否可以?如果是这样,怎么办?
提前致谢
sizes.thumbnail.url使用url 属性和 using 的值创建一个对象_.get(),并将其与_.pick().
注意:我使用object spread来合并结果,但您可以使用Object.assign()or 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);Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>Run Code Online (Sandbox Code Playgroud)
更通用的解决方案是一个接受路径列表的函数,并生成一个数组对[路径的最后一部分,值]。_.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);Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>Run Code Online (Sandbox Code Playgroud)