【问题标题】:JS: Extract unique strings from multiple array elementsJS:从多个数组元素中提取唯一字符串
【发布时间】:2017-12-12 08:37:27
【问题描述】:

我的对象数组的数据结构如下所示:

const data = [ 
  { 
    _id: 'Dn59y87PGhkJXpaiZ',
    title: 'Sample Article',
    slug: 'sample-article',
    created: 1503160075
  },
  { 
    _id: 'ujJCBC2avK8QkR86t',
    content: 'Lorem ipsum',
    parent: 'Dn59y87PGhkJXpaiZ'
    reference: [ '9Z7k2wAbXNXY2JWuE' ],
    timestamp: 1513054017
  },
  { 
    _id: 'KRhcfZSWFAawfxAsj',
    content: 'Lorem ipsum',
    parent: 'Dn59y87PGhkJXpaiZ',
    reference: [ '8vtFExXqEF4Hghx2b' ],
    timestamp: 1512864671
  }
]

现在我需要获取所有唯一的参考字符串,我试图这样做:

const result = data.filter(doc => doc.reference).map(doc => doc.reference)

但这给了我结果......

[ [ '9Z7k2wAbXNXY2JWuE' ], [ '8vtFExXqEF4Hghx2b' ] ]

...我期待类似的东西

[ '9Z7k2wAbXNXY2JWuE', '8vtFExXqEF4Hghx2b' ]

我还需要消除重复项(此示例数据中未显示)。

【问题讨论】:

    标签: javascript ecmascript-6 lodash


    【解决方案1】:

    使用array.prototype.reduce连接引用数组,Set避免重复,spread operator转换Set em> 返回数组:

    var data = [ 
      { 
        _id: 'Dn59y87PGhkJXpaiZ',
        title: 'Sample Article',
        slug: 'sample-article',
        created: 1503160075
      },
      { 
        _id: 'ujJCBC2avK8QkR86t',
        content: 'Lorem ipsum',
        parent: 'Dn59y87PGhkJXpaiZ',
        reference: [ '9Z7k2wAbXNXY2JWuE', '9Z7k2wAbXNXY2JWuE' ],
        timestamp: 1513054017
      },
      { 
        _id: 'KRhcfZSWFAawfxAsj',
        content: 'Lorem ipsum',
        parent: 'Dn59y87PGhkJXpaiZ',
        reference: [ '8vtFExXqEF4Hghx2b', '8vtFExXqEF4HgDx2b' ],
        timestamp: 1512864671
      }
    ];
    
    var result = [ ...new Set(data.reduce((m, doc) => m.concat(doc.reference || []), []))];
    
    console.log(result);

    【讨论】:

      【解决方案2】:

      您需要使用map 提取引用然后展平数组:

      const data = [ 
        { 
          _id: 'Dn59y87PGhkJXpaiZ',
          title: 'Sample Article',
          slug: 'sample-article',
          created: 1503160075
        },
        { 
          _id: 'ujJCBC2avK8QkR86t',
          content: 'Lorem ipsum',
          parent: 'Dn59y87PGhkJXpaiZ',
          reference: [ '9Z7k2wAbXNXY2JWuE' ],
          timestamp: 1513054017
        },
        { 
          _id: 'KRhcfZSWFAawfxAsj',
          content: 'Lorem ipsum',
          parent: 'Dn59y87PGhkJXpaiZ',
          reference: [ '8vtFExXqEF4Hghx2b' ],
          timestamp: 1512864671
        }
      ]
      
      console.log(...[].concat(...data.map(d => d.reference || [])));

      【讨论】:

        【解决方案3】:

        就这样做

        let result = data.filter(doc => doc.reference).map(doc => doc.reference)[0]
        result = [ ...result ]
        

        展开运算符会将其展开为一个数组。

        【讨论】:

        • result = [["9Z7k2wAbXNXY2JWuE"], ["8vtFExXqEF4Hghx2b"]]
        • 我已经更新了答案,请看一下结果过滤。当您映射对象时,只需在每次数组执行map 时获取第一个索引
        【解决方案4】:

        使用reduce 代替map

        data.filter(doc => doc.reference).reduce( (a,b ) => a.concat( b.reference ),  [] );
        

        演示

        var data = [ 
          { 
            _id: 'Dn59y87PGhkJXpaiZ',
            title: 'Sample Article',
            slug: 'sample-article',
            created: 1503160075
          },
          { 
            _id: 'ujJCBC2avK8QkR86t',
            content: 'Lorem ipsum',
            parent: 'Dn59y87PGhkJXpaiZ',
            reference: [ '9Z7k2wAbXNXY2JWuE' ],
            timestamp: 1513054017
          },
          { 
            _id: 'KRhcfZSWFAawfxAsj',
            content: 'Lorem ipsum',
            parent: 'Dn59y87PGhkJXpaiZ',
            reference: [ '8vtFExXqEF4Hghx2b' ],
            timestamp: 1512864671
          }
        ];
        var output = data.filter(doc => doc.reference).reduce( (a,b ) => a.concat(b.reference),  [] );
        
        console.log( output );

        或者如果引用数组中只有一项

        data.filter( doc => doc.reference ).map( a => a[0] );
        

        编辑

        现在我需要获取我试图获取的所有唯一引用字符串 这样做:

        如果字符串必须是唯一的,则使用此版本

        data.reduce( function( a, b ) {
           if ( b.reference ) 
           {
               a = a.concat(b.filter( i => a.indexOf(i) == -1 ));
           }
           return a;
        } , [] );
        

        带箭头功能

        data.reduce( ( a, b ) => ( b.reference ? a.concat( b.reference.filter( i => a.indexOf( i ) == -1 ) ) : a) , [] );
        

        演示

        var data = [ 
          { 
            _id: 'Dn59y87PGhkJXpaiZ',
            title: 'Sample Article',
            slug: 'sample-article',
            created: 1503160075
          },
          { 
            _id: 'ujJCBC2avK8QkR86t',
            content: 'Lorem ipsum',
            parent: 'Dn59y87PGhkJXpaiZ',
            reference: [ '9Z7k2wAbXNXY2JWuE' ],
            timestamp: 1513054017
          },
          { 
            _id: 'KRhcfZSWFAawfxAsj',
            content: 'Lorem ipsum',
            parent: 'Dn59y87PGhkJXpaiZ',
            reference: [ '8vtFExXqEF4Hghx2b' ],
            timestamp: 1512864671
          }
        ];
        var output = data.reduce( ( a, b ) => ( b.reference ? a.concat( b.reference.filter( i => a.indexOf( i ) == -1 ) ) : a) , [] );
        console.log( output );

        【讨论】:

        • 如果 OP 需要正确映射,Reduce 绝对是矫枉过正。而且更难阅读和理解。
        • @dfsq 更难阅读和理解同意,如果你以前没有用过的话。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-14
        • 2016-04-18
        • 2021-04-07
        • 2019-03-04
        • 2018-12-31
        • 1970-01-01
        相关资源
        最近更新 更多