オブジェクトのキー名を正規表現で調べるconst data = {id1: 100, name: 'foo', id2: 200}; const val = (() => { const key = Object.keys(data).find(x => x.match(/^id[0-9]/)); return data[key]; })(); console.log(val); => 100 const data = {id1: 100, name: 'foo', id2: 200}; const val = data[Object.keys(data).find(x => x.match(/^id[0-9]/))]; console.log(val); => 100 参考 |
|