Javascript 嵌套数组中的lodash拾取

Javascript 嵌套数组中的lodash拾取,javascript,arrays,lodash,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,

我有这样一个对象数组:

{
  "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":""
}
我正在使用以下代码片段创建一个新数组,其中只包含数组中的
alt
caption
id
url
键:

images.map( ( image ) => pick( image, [ 'alt', 'caption', 'id', 'url' ] ) ),
我的问题是,如何选择
size.缩略图.url
键而不是根
url
键?可能吗?如果是,怎么做


提前感谢

使用创建一个具有url属性和值
size.thumboil.url
的对象,并将其与组合到
\uuu.pick()
的结果中

注意:我曾用于合并结果,但您可以使用或lodash的等效项

const images=[{“大小”:{“缩略图”:{“高度”:300,“宽度”:300,“url”:http://example.com/wp-content/uploads/2017/04/web-300x300.jpg,“方向”:“横向”},“中等”:{“高度”:267,“宽度”:400,“url”:http://example.com/wp-content/uploads/2017/04/web-400x267.jpg,“方向”:“横向”},“大”:{“高度”:441,“宽度”:660,“url”:"http://example.com/wp-content/uploads/2017/04/web-1024x684.jpg,“方向”:“横向”},“完整”:{“url”:http://example.com/wp-content/uploads/2017/04/web.jpg,“高度”:1200,“宽度”:1796,“方向”:“横向”},“mime”:“图像/jpeg”,“类型”:“图像”,“子类型”:“jpeg”,“id”:3589,“url”:http://example.com/wp-content/uploads/2017/04/web.jpg,“alt”:,“链接”:http://example.com/web/“,”标题“:”}];
const result=images.map((图像)=>({
选择(图像,['alt','caption','id']),
url:u2;.get(图像,'size.缩略图.url')
}));
控制台日志(结果)
您可以使用纯js

函数pickfromobject(对象,键){
var parts=key.split(“.”)
如果(零件长度>1){
var tempkey=parts.shift()
如果(!对象[tempkey]){
返回空
}否则{
返回pickfromobject(对象[tempkey],parts.join('.'))
}
}否则{
返回对象[部件[0]]
}
}
功能拾取(arr,键){
var结果=[]
对于(var i=0;i
var数组=[{“大小”:{“缩略图”:{“高度”:300,“宽度”:300,“url”:http://example.com/wp-content/uploads/2017/04/web-300x300.jpg,“方向”:“横向”},“中等”:{“高度”:267,“宽度”:400,“url”:http://example.com/wp-content/uploads/2017/04/web-400x267.jpg,“方向”:“横向”},“大”:{“高度”:441,“宽度”:660,“url”:"http://example.com/wp-content/uploads/2017/04/web-1024x684.jpg,“方向”:“横向”},“完整”:{“url”:http://example.com/wp-content/uploads/2017/04/web.jpg,“高度”:1200,“宽度”:1796,“方向”:“横向”},“mime”:“图像/jpeg”,“类型”:“图像”,“子类型”:“jpeg”,“id”:3589,“url”:http://example.com/wp-content/uploads/2017/04/web.jpg,“alt”:,“链接”:http://example.com/web/“,”标题“:”}];
var result=\.map(数组,v=>({“alt”:v.alt,“caption”:v.caption,“id”:v.id,“url”:v.size.缩略图.url});
console.log(结果);

我认为这是一个非常详细的例子,其中包含了更多的bug表面积。在大多数情况下,最好使用Lodash或下划线选项,你应该为此获得奖牌!