lodash选择嵌套数组

时间:2018-04-30 07:47:24

标签: javascript arrays lodash

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

UPDATE `car` SET `attributes` = JSON_REMOVE(`attributes`, '$.B');

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

url

我的问题是,如何选择images.map( ( image ) => pick( image, [ 'alt', 'caption', 'id', 'url' ] ) ), 密钥而不是根sizes.thumbnail.url密钥?可能吗?如果是这样,怎么样?

提前致谢

3 个答案:

答案 0 :(得分:1)

使用_.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>

答案 1 :(得分:0)

var array = [{
  "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":""
}];


var result = _.map(array, v => ({"alt":v.alt, "caption":v.caption, "id":v.id, "url":v.sizes.thumbnail.url}));

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

答案 2 :(得分:-1)

您可以使用普通的js,

&#13;
&#13;
historyIer.Next()
&#13;
&#13;
&#13;