【问题标题】:How to build new array in underscore/lodash如何在下划线/lodash中构建新数组
【发布时间】:2015-08-06 00:03:10
【问题描述】:

我有以下 JSON 数据,包含嵌套数组

{
    "works": {
        "work": [
            {
                "id": "001",
                "filename": "image1.jpg",
                "urls": {   
                    "url": [
                        {
                            "_type": "small",
                            "__text": "small-image.jpg"
                        },
                        {
                            "_type": "medium",
                            "__text": "medium-image.jpg"
                        },
                        {
                            "_type": "large",
                            "__text": "large-image.jpg"
                        }
                    ]
                },
                "details": {
                    "flash": "0",
                    "model": "CAMERA MODEL 1",                    
                    "make": "CAMERA MAKE 1",                   
                }
            },

            {
                "id": "001",
                "filename": "image1.jpg",
                "urls": {   
                    "url": [
                        {
                            "_type": "small",
                            "__text": "small-image.jpg"
                        },
                        {
                            "_type": "medium",
                            "__text": "medium-image.jpg"
                        },
                        {
                            "_type": "large",
                            "__text": "large-image.jpg"
                        }
                    ]
                },
                "details": {
                    "flash": "0",
                    "model": "CAMERA MODEL 2",                    
                    "make": "CAMERA MAKE 2",                   
                }
            }                     
        ]
    }
}

我想重写数组,让它看起来像这样

[{
    "id": "001",
    "filename": "image1.jpg",
    "_type": "small",
    "url": "small-image.jpg",
    "model": "CAMERA MODEL 1",                    
    "make": "CAMERA MAKE 1"  
}, {
    "id": "002",
    "filename": "image2.jpg",
    "_type": "small",
    "url": "small-image.jpg",
    "model": "CAMERA MODEL 2",                    
    "make": "CAMERA MAKE 2"  
}]

我一直在阅读有关 Lodash 中的链接的信息,但到目前为止,实施它的运气并不好。到目前为止,这是我所得到的:

var flattened = _.chain(data).pluck("works")
         .concat(_.flatten(_(data).pluck("work")))
         .reject(_.isUndefined)
         .value()

任何帮助将不胜感激!

【问题讨论】:

  • work 数组的两个成员都有"id": "001",但输出有"001""002",这是错字吗?

标签: javascript arrays json underscore.js lodash


【解决方案1】:

不确定您对使用纯链接有多在意,但如果您只关心将数据从一种格式转换为另一种格式,那么使用 lodash 地图可能会奏效。

_(source.works.work).map(function(item){
  var image = _(item.urls.url).find({'_type': 'small'});
  return {
    id: item.id,
    filename: item.filename,
    _type: image ? image._type : null,
    url: image ? image.__text : null,
    model: item.details.model,
    make: item.details.make
  };
}).value();

这将导致:

[ { id: '001',
    filename: 'image1.jpg',
    _type: 'small',
    url: 'small-image.jpg',
    model: 'CAMERA MODEL 1',
    make: 'CAMERA MAKE 1' },
  { id: '001',
    filename: 'image1.jpg',
    _type: 'small',
    url: 'small-image.jpg',
    model: 'CAMERA MODEL 2',
    make: 'CAMERA MAKE 2' } ]

【讨论】:

  • 为什么是reduce 而不是map
  • 非常感谢 datfinesoul - 我尝试实现,但收到错误 Uncaught TypeError: Cannot read property 'work' of undefined。如果我 console.log(data) 虽然,数据被加载。再次感谢。
  • Bryan 是正确的,地图将是更好的答案,我回答时太累了。我已经更新了上面的代码。 Desmond,这是答案的 JSBin 链接。 jsbin.com/qareqatiri/1/embed?js,console
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-17
  • 2018-05-04
  • 1970-01-01
  • 2021-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多