JS:从多个数组元素中提取唯一的字符串

时间:2017-12-12 08:37:27

标签: javascript ecmascript-6 lodash

这是我的对象数组的数据结构的样子:

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' ]

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

4 个答案:

答案 0 :(得分:0)

使用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 );

答案 1 :(得分:0)

就这样做

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

传播运算符会将它扩展为数组。

答案 2 :(得分:0)

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

&#13;
&#13;
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 || [])));
&#13;
&#13;
&#13;

答案 3 :(得分:0)

使用 array.prototype.reduce 连接引用数组,设置以避免重复,传播运算符转换 Set 回到数组:

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);