简体   繁体   English

将嵌套数组转换为 JS 中的字符串数组

[英]Convert Nested Array into Array of Strings in JS

I need to extract the supplier property from nested arrays.我需要从嵌套数组中提取supplier属性。 Is there a better and simpler way to do this?有没有更好更简单的方法来做到这一点? Only display it once if it has duplicates如果有重复,只显示一次

Expected Output should be like:预期输出应如下所示:

['Moscow', 'USA']

 const oldData = [ { "uid": "AA1", "members": [ { "id": 123, "createdTs": "2018-11-07T04:55:00.000+00:00", "modifiedTs": "2022-03-17T23:29:06.000+00:00", "uid": "d@yahoo.com", "name": "Dayanara", "active": true, "lastLogin": "2020-10-28T03:22:22.000+00:00", "supplier": "Moscow" }, { "id": 456, "createdTs": "2018-10-28T22:42:57.000+00:00", "modifiedTs": "2020-06-01T05:01:11.000+00:00", "uid": "j@yahoo.com", "name": "John Jones", "active": true, "lastLogin": "2020-06-01T05:00:35.000+00:00", "supplier": null }, { "id": 789, "createdTs": "2022-01-28T05:21:37.000+00:00", "modifiedTs": "2022-02-04T06:24:54.000+00:00", "uid": "g@gmail.com", "name": "Gasmund", "active": true, "lastLogin": null, "supplier": "" } ] }, { "uid": "AA2", "members": [ { "id": 10112, "createdTs": "2022-07-07T09:51:14.000+00:00", "modifiedTs": "2022-07-07T09:51:14.000+00:00", "uid": "aa@yahoo.com", "name": "deqwd", "active": true, "lastLogin": null, "supplier": "USA" }, { "id": 101123, "createdTs": "2022-07-07T09:51:14.000+00:00", "modifiedTs": "2022-07-07T09:51:14.000+00:00", "uid": "aa33@yahoo.com", "name": "fewfewffwef", "active": true, "lastLogin": null, "supplier": "USA" } ] } ] const newData = oldData.flatMap((groupUsers) => ( groupUsers.members.map(({ supplier }) => ({ supplier: supplier })) )); console.log(newData)

A double Array.flatMap will help.Array.flatMap会有所帮助。

If you have duplicates and expect unique list as output, use can use Set如果您有重复项并期望唯一列表作为输出,则可以使用Set

First flatMap to generate a list of members, second flatMap to generate the list of supplier and a Set to generate the unique list from this.第一个flatMap生成成员列表,第二个flatMap生成供应商列表和一个 Set 从中生成唯一列表。

 const oldData = [ { uid: "AA1", members: [ { id: 123, createdTs: "2018-11-07T04:55:00.000+00:00", modifiedTs: "2022-03-17T23:29:06.000+00:00", uid: "d@yahoo.com", name: "Dayanara", active: true, lastLogin: "2020-10-28T03:22:22.000+00:00", supplier: "Moscow" }, { id: 456, createdTs: "2018-10-28T22:42:57.000+00:00", modifiedTs: "2020-06-01T05:01:11.000+00:00", uid: "j@yahoo.com", name: "John Jones", active: true, lastLogin: "2020-06-01T05:00:35.000+00:00", supplier: null }, { id: 789, createdTs: "2022-01-28T05:21:37.000+00:00", modifiedTs: "2022-02-04T06:24:54.000+00:00", uid: "g@gmail.com", name: "Gasmund", active: true, lastLogin: null, supplier: "" }, ], }, { uid: "AA2", members: [ { id: 10112, createdTs: "2022-07-07T09:51:14.000+00:00", modifiedTs: "2022-07-07T09:51:14.000+00:00", uid: "aa@yahoo.com", name: "deqwd", active: true, lastLogin: null, supplier: "USA" }, ], }, ]; const newData = oldData .flatMap(({ members }) => members) .flatMap(({ supplier }) => (supplier ? supplier : [])); const uniqueSet = Array.from(new Set(newData)); console.log(newData); console.log(uniqueSet);

You could use filter to exclude the empty strings, flatMap to collect the rest of the values, and Set to get unique values:您可以使用filter排除空字符串,使用flatMap收集其余值,并使用Set获取唯一值:

 const oldData = [{"members": [{"supplier": "Moscow"},{"supplier": null},{"supplier": ""}]},{"members": [{"supplier": "USA"},{"supplier": "USA"}]}]; const newData = Array.from(new Set( oldData.flatMap(({members}) => members.map(({supplier}) => supplier).filter(Boolean) ) )); console.log(newData);

Alternative approach with Array.prototype.reduce : Array.prototype.reduce的替代方法:

const newData = Array.from(new Set(oldData
  .reduce((p, c) => p.concat(c.members.filter(m => !!m.supplier)) , [])
  .reduce((p, c) => p.concat(c.supplier), [])
));                    

You could map just the supplier value and filter with a Set for truthy values.您可以仅映射supplier值并使用Set过滤以获取真实值。

 const oldData = [{ uid: "AA1", members: [{ id: 123, createdTs: "2018-11-07T04:55:00.000+00:00", modifiedTs: "2022-03-17T23:29:06.000+00:00", uid: "d@yahoo.com", name: "Dayanara", active: true, lastLogin: "2020-10-28T03:22:22.000+00:00", supplier: "Moscow" }, { id: 456, createdTs: "2018-10-28T22:42:57.000+00:00", modifiedTs: "2020-06-01T05:01:11.000+00:00", uid: "j@yahoo.com", name: "John Jones", active: true, lastLogin: "2020-06-01T05:00:35.000+00:00", supplier: null }, { id: 789, createdTs: "2022-01-28T05:21:37.000+00:00", modifiedTs: "2022-02-04T06:24:54.000+00:00", uid: "g@gmail.com", name: "Gasmund", active: true, lastLogin: null, supplier: "" }] }, { uid: "AA2", members: [{ id: 10112, createdTs: "2022-07-07T09:51:14.000+00:00", modifiedTs: "2022-07-07T09:51:14.000+00:00", uid: "aa@yahoo.com", name: "deqwd", active: true, lastLogin: null, supplier: "USA" }, { id: 101123, createdTs: "2022-07-07T09:51:14.000+00:00", modifiedTs: "2022-07-07T09:51:14.000+00:00", uid: "aa33@yahoo.com", name: "fewfewffwef", active: true, lastLogin: null, supplier: "USA" }] }], newData = oldData .flatMap((groupUsers) => groupUsers.members.map(({ supplier }) => supplier)) .filter((s => v => v && !s.has(v) && s.add(v))(new Set)); console.log(newData);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM