エンジニアとしての市場価値を測りませんか?PR

企業からあなたに合ったオリジナルのスカウトを受け取って、市場価値を測りましょう

0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JSでオブジェクトのリストからあるキーを持つオブジェクトを抽出したい

Last updated at Posted at 2019-01-25

目的

オブジェクトのリストからあるキーを持つオブジェクトを抽出したい。
具体的には以下のデータ構造のものからキーのリストを与えて、いずれかのキーを持つリストを返してほしい。

// 抽出元のデータ
let data_list = [
    {"code":"01", "value":"a"},
    {"code":"02", "value":"b"},
    {"code":"03", "value":"c"},
    {"code":"04", "value":"d"},
    {"code":"05", "value":"e"},
    {"code":"06", "value":"f"},
];

// 抽出したいキーの値のリスト
let code_list = ["01", "02", "03"];

実装

いちいちfor文を回したくない
以下のように実装した

// filter関数で条件の関数がtrueになるものだけを抽出
let dst_data = data_list.filter(
    (element) => {
        // 要素のキーが抽出したいキーのリストに含まれているならtrue
        return code_list.includes(element.code);
    }
);

console.log(dst_data);

参考リンク

Array.prototype.includes()
JavaScriptでforEach, filter, map, reduceとか

0
0
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

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?