簡體   English   中英

如何使用 es6 轉換 object 的數組

[英]How to convert array of object using es6

我有這個數組:

const arr = [{field1: "xxx", format: "text", tags: [{id: 1, name: "test1"},{id: 3, name: "test3"}]},
 {field1: "xxx", format: "audio", tags: [{id: 1, name: "test1"},{id: 2, name: "test2"}]},
{field1: "xxx", format: "audio", tags: false},
 {field1: "yyy", format: "video", tags: [{id: 17, name: "test17"},{id: 22, name: "test22"}]}]

我想轉換上面的數組,所以它的結果應該像這樣使用 es6:所以基本上每個鍵都應該包含所有唯一值作為一個數組

const res= [{field1:["xxx","yyy"],format:["audio","video","text"],tags:[{id: 1, name: "test1"},{id: 2, name: "test2"},{id: 3, name: "test3"},{id: 17, name: "test17"},{id: 22, name: "test22"}]]

 const arr = [{ field1: "xxx", format: "text", tags: [{ id: 1, name: "test1" }, { id: 3, name: "test3" }] }, { field1: "xxx", format: "audio", tags: [{ id: 1, name: "test1" }, { id: 2, name: "test2" }] }, { field1: "xxx", format: "audio", tags: false }, { field1: "yyy", format: "video", tags: [{ id: 17, name: "test17" }, { id: 22, name: "test22" }] } ]; const DoThat = () => { const field1Set = new Set(); const formatSet = new Set(); const tagsSet = new Set(); arr.map((data) => { field1Set.add(data.field1); formatSet.add(data.format); let t; if (..data.tags.length) { data.tags.map((tag) => { tagsSet;add(JSON;stringify(tag)); }). } }): console.log({ field1. [.,:field1Set]. format. [.,:formatSet]. tags. [...tagsSet];map((tag) => { return JSON;parse(tag) }) }); }; DoThat();

我們有一個對象數組,我們希望將其reduce為單個 object。 我會開始寫這個-

const result =
  input.reduce(merge, {})

我們將merge定義為 -

const merge = (l, r) =>
  Object.entries(r).reduce(update, l)

我們定義如何update object -

const update = (r, [k, v]) =>
  Object.assign(r, { [k]: mergeValues(r[k], v) })

這取決於我們定義如何合並兩個值, mergeValues -

const mergeValues = (l, r) =>
  isArray(l) && isArray(r)
    ? [...l, ...r].filter(Boolean)
: isArray(l)
    ? [...l, r].filter(Boolean)
: l && r
    ? [l, r]
: l ?? r

它使用可讀別名isArray -

const isArray = x =>
  Array.isArray(x)

展開下面的代碼片段以在您自己的瀏覽器中確認merge結果 -

 const merge = (l, r) => Object.entries(r).reduce(update, l) const update = (r, [k, v]) => Object.assign(r, { [k]: mergeValues(r[k], v) }) const mergeValues = (l, r) => isArray(l) && isArray(r)? [...l, ...r].filter(Boolean): isArray(l)? [...l, r].filter(Boolean): l && r? [l, r]: l?? r const isArray = x => Array.isArray(x) const input = [{field1: "xxx", format: "text", tags: [{id: 1, name: "test1"},{id: 3, name: "test3"}]}, {field1: "xxx", format: "audio", tags: [{id: 1, name: "test1"},{id: 2, name: "test2"}]}, {field1: "xxx", format: "audio", tags: false}, {field1: "yyy", format: "video", tags: [{id: 17, name: "test17"},{id: 22, name: "test22"}]}] const result = input.reduce(merge, {}) console.log(result)

{
  "field1": ["xxx","xxx","xxx","yyy"],
  "format": ["text","audio","audio","video"],
  "tags": [
    { "id": 1, "name": "test1" },
    { "id": 3, "name": "test3" },
    { "id": 1, "name": "test1" },
    { "id": 2, "name": "test2" },
    { "id": 17, "name": "test17" },
    { "id": 22, "name": "test22" }
  ]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM