JS循環遍歷數組並用地圖分組

賽博J

我有數組terms

[
    {
        "label": "programming",
        "id": 6,
        "taxonomy": "category"
    },
    {
        "label": "design",
        "id": 4,
        "taxonomy": "post_tag"
    },
    {
        "label": "css",
        "id": 3,
        "taxonomy": "post_tag"
    },
    {
        "label": "other",
        "id": 8,
        "taxonomy": "category"
    }
]

我正在嘗試newArray從數組創建以下數組terms

[
  {
      "taxonomy": "category",
      "ids": [6, 8]
  },
  {
      "taxonomy": "post_tag",
      "ids": [4, 3]
  },
]

我必須idterms數組中的每個對像中取出並將它們添加到數組中相應的分類法中newArray

我正在嘗試以下操作:

const taxonomyArray = [];
const newArray = [];

// Loop through original term array and get the taxonomies
terms.forEach( el => {
   taxonomyArray.push( el['taxonomy'] )
});

// Remove duplicate taxonomy values from array taxonomyArray
const uniqueTaxonomyArray = [ ...new Set(taxonomyArray) ];

// Create the new array
uniqueTaxonomyArray.forEach( el => {
    const groupedArray = terms
    .filter( ( { taxonomy } ) => taxonomy === el )
    .map( ( { value, taxonomy } ) => ( {
       taxonomy: el,
       ids: value
    } ) );

    newArray.push( groupedArray );
});

它給了我:

[
    [
        {
            "taxonomy": "category",
            "ids": 6
        },
        {
            "taxonomy": "category",
            "ids": 8
        }
    ],
    [
        {
            "taxonomy": "post_tag",
            "ids": 4
        },
        {
            "taxonomy": "post_tag",
            "ids": 3
        }
    ]
]

有沒有更好的方法來實現這一目標?

前谷

Array.reduceMap

const terms = [ { "label": "programming", "id": 6, "taxonomy": "category" }, { "label": "design", "id": 4, "taxonomy": "post_tag" }, { "label": "css", "id": 3, "taxonomy": "post_tag" }, { "label": "other", "id": 8, "taxonomy": "category" }];

const map = terms.reduce((acc, {id, taxonomy}) => {
  if (acc.has(taxonomy)) {
    acc.get(taxonomy).ids.push(id);
  } else {
    acc.set(taxonomy, {taxonomy: taxonomy, ids: [id]});
  }
  return acc;
}, new Map());

const result = [...map.values()];

console.log(result);

降低

reducer 逐個元素遍歷數組,在每一步將當前數組值添加到上一步的結果(此結果是所有先前步驟的運行總和)——直到沒有更多元素要添加。

// Arrow function
reduce((previousValue, currentValue) => { ... } )
reduce((previousValue, currentValue, currentIndex) => { ... } )
reduce((previousValue, currentValue, currentIndex, array) => { ... } )
reduce((previousValue, currentValue, currentIndex, array) => { ... }, initialValue)

// Callback function
reduce(callbackFn)
reduce(callbackFn, initialValue)

// Inline callback function
reduce(function(previousValue, currentValue) { ... })
reduce(function(previousValue, currentValue, currentIndex) { ... })
reduce(function(previousValue, currentValue, currentIndex, array) { ... })
reduce(function(previousValue, currentValue, currentIndex, array) { ... }, initialValue)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

循環遍歷數組和數據幀

Javascript 對象 - 檢查並循環遍歷對像中的數組

循環遍歷數組並檢查多個條件

jquery:循環遍歷 json 數組

循環遍歷嵌套的對像數組

如何從嵌套數組中刪除數組,循環遍歷數組的其餘部分。然後循環遍歷嵌套數組的 1 個值,全部在 vanilla js 中

循環遍歷嵌套數組並從第一個循環數組中返回包含字符串的項

循環遍歷兩個數組以填充二維數組

在 SwiftUI 中循環遍歷實體數組以基於數組數據中的值構建樹結構視圖

#each 循環中的 #reduce 循環不會遍歷整個數組

循環遍歷數組並在 Javascript/Nodejs 中輸出模板字符串/文字

如何從當前日期開始循環遍歷天數數組

如何循環遍歷具有不同數據類型的集合/映射數組

Javascript - 如何在 React 中一遍又一遍地循環遍歷數組

如何循環遍歷包含對象和數組的 JSON 對象

循環遍歷嵌套數組以返回對象的值

循環遍歷二維數組 | 電源外殼

循環遍歷結構體數組時打印的值不正確

循環遍歷 JSON 數組以獲取隨機值(Javascript)

打字稿 - 在啟用 noUncheckedIndexedAccess 的情況下向後循環遍歷數組

如何在 Java 中的一個循環中遍歷 2 個不同的數組

循環遍歷隨機生成的數組的最佳方法

如何從文件循環遍歷多行數組並將每一行分配給多個參數?

循環遍歷列的成對組合

循環遍歷 ArrayList 的所有可能組合

Terraform 循環遍歷 2 個元組

python 3.x循環遍歷數組找到重複元素並將它們相加為一個新元素

循環遍歷對象並根據給定數組中的正則表達式替換值

遍歷數組並添加數據