0
1

More than 1 year has passed since last update.

配列の中のオブジェクト中の配列を重複がないように、二次元配列から一次元配列として取得する

Last updated at Posted at 2022-10-24

やりたいこと

このtagsを

const customers = [
  {
    lastName: '2f8',
    tags: ['VIP', 'male'],
  },
  {
    email: 'a@gmail.com',
    tags: ['VIP', 'male'],
  },
  {
    email: 'c@gmail.com',
    tags: ['VIP', 'female'],
  },
];


重複のないような一次元配列にしたい

['VIP', 'male','female']

解決方法

const customers = [
  {
    lastName: '2f8',
    tags: ['VIP', 'male'],
  },
  {
    email: 'a@gmail.com',
    tags: ['VIP', 'male'],
  },
  {
    email: 'c@gmail.com',
    tags: ['VIP', 'female'],
  },
];

const array = customers.map((el) => el.tags.map((el) => el));
const flatArray = [].concat(...array);
const noDoubleArray = Array.from(new Set(flatArray));

0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1