-
Notifications
You must be signed in to change notification settings - Fork 0
/
myFunction3_2.gs
33 lines (26 loc) · 1.02 KB
/
myFunction3_2.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function myFunction3_2() {
//元データ
const members = [
{ id: "tg001", name: "Tsujike", address: "Hokkaido" },
{ id: "tg002", name: "Takahashi", address: "Fukuoka" },
{ id: "tg003", name: "Etau", address: "Tokyo" },
];
//プロパティのフィルターに必要な条件要素の配列
const propertiesFilter = ["name", "id"];
//membersを必要なプロパティのみでmapする
const filteredProperties = members.map(member => {
//空のオブジェクト生成とレコードのプロパティを都度配列に格納
const keys = Object.keys(member);
const newObj = {};
//条件要素の配列に対してループ処理
propertiesFilter.forEach(property => {
if (keys.includes(property)) newObj[property] = member[property];
});
// できあがったオブジェクトをmapにreturn
return newObj;
});
console.log(filteredProperties);
// [ { name: 'Tsujike', id: 'tg001' },
// { name: 'Takahashi', id: 'tg002' },
// { name: 'Etau', id: 'tg003' } ]
}