簡體   English   中英

Javascript 或 Ramda 變換 JSON 按屬性

[英]Javascript or Ramda Transform JSON By Attributes

我在我的項目中使用Ramda 庫

是否可以按照 JSON 陣列進行轉換

[
   {
     "id": 1,
     "name": "test",
   },
   {
     "id": 2,
     "name": "test2"
   }

];

[
   {
     "id": 1,
     "id": 2,

   },
   {
     "name": "test",
     "name": "test2"
   }

];

請幫忙

對象不能有多個具有相同鍵的屬性,因此{ "id": 1, "id": 2 }{ "name": "test", "name": "test2" }無效。 我假設您需要一個 id 數組和一個名稱數組:

[[1, 2, 3], ['test', 'test2', 'test3']]

如果所有對象都具有相同的鍵順序 - 即沒有{ id: 1, name: 'test'}{ name: 'test2', id: 1 } ,並且您需要 object 中的所有值,則可以 map對象到它們的值,然后轉置:

 const { pipe, map, values, transpose } = R; const fn = pipe( map(values), transpose, ); const arr = [{"id":1,"name":"test"},{"id":2,"name":"test2"},{"id":3,"name":"test3"}]; const result = fn(arr); console.log(result);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js" integrity="sha512-rZHvUXcc1zWKsxm7rJ8lVQuIr1oOmm7cShlvpV0gWf0RvbcJN6x96al/Rp2L2BI4a4ZkT2/YfVe/8YvB2UHzQw==" crossorigin="anonymous"></script>

如果某些對象有不同的鍵插入順序,您想更改生成的 arrays 的順序,或者如果您需要某些鍵,您可以使用 R.props 獲取值,然后轉置:

 const { pipe, map, props, transpose } = R; const fn = pipe( map(props(['name', 'id'])), // example - name would be the 1st sub-array transpose, ); const arr = [{"id":1,"name":"test"},{"id":2,"name":"test2"},{"id":3,"name":"test3"}]; const result = fn(arr); console.log(result);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js" integrity="sha512-rZHvUXcc1zWKsxm7rJ8lVQuIr1oOmm7cShlvpV0gWf0RvbcJN6x96al/Rp2L2BI4a4ZkT2/YfVe/8YvB2UHzQw==" crossorigin="anonymous"></script>

如果您想要 Scott Sauyet 建議的結構:

{
  id: [1, 2],
  name: ['test1', 'test2']
}

I would map and flatten the objects to an array of pairs with R.chain and R.toPairs, group them by the 1st item in each pair (the original key), and then map each groups item to the last item in each pair (原始值)。

 const { pipe, chain, toPairs, groupBy, head, map, last } = R const fn = pipe( chain(toPairs), groupBy(head), map(map(last)), // map(pipe(map(last), uniq)) if you want only unique items ) const arr = [{"id":1,"name":"test"},{"id":2,"name":"test2"},{"id":3,"name":"test3"}]; console.log(fn(arr))
 <script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.js"></script>

正如 OriDrori 指出的那樣,您請求的 output 不是有效的 JS。 不過,對於它的一個有效變體,我將做出一個稍微不同的猜測,即我們想要一個像這樣的 output:

{
  id: [1, 2],
  name: ['test1', 'test2']
}

這是在 vanilla JS 中實現這一目標的一種簡單方法:

 const extract = (data) => { const keys = [... new Set (data.flatMap (Object.keys))] return Object.fromEntries ( keys.map (k => [k, data.map (o => o [k])]) ) } const data = [{id: 1, name: "test"}, {id: 2, name: "test2"}] console.log ( extract (data) )

我們絕對可以用 Ramda 函數來清理它。 另一個版本可能如下所示:

 const extract = (data) => fromPairs ( map (k => [k, map (o => o [k], data)]) (uniq (chain (keys) (data))) ) const data = [{id: 1, name: "test"}, {id: 2, name: "test2"}] console.log (extract (data))
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.js"></script> <script> const {fromPairs, map, uniq, chain, keys} = R </script>

雖然我們可以 go 完全無點,但我發現這個版本的可讀性要差得多:

 const extract = compose ( fromPairs, lift (map) ( unary (compose (ap (pair), flip (pluck))), compose (uniq, chain (keys)) ) ) const data = [ {id: 1, name: "test"}, {id: 2, name: "test2"} ] console.log (extract (data))
 <script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.js"></script> <script> const {compose, fromPairs, lift, map, unary, ap, pair, flip, pluck, uniq, chain, keys} = R </script>

暫無
暫無

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

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