跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • Js 怎么将 1 种数组转换为另一种数组?
未分類
21 5 月 2020

Js 怎么将 1 种数组转换为另一种数组?

Js 怎么将 1 种数组转换为另一种数组?

資深大佬 : LionXen 52

请教大佬。

array[0][name]:apple
array[0][price]:55.00
array[0][tips]:red apple
array[1][name]:orange
array[1][price]:41.13
array[1][tips]:4orange

如何转换为:

array[name][0]:apple
array[name][1]:orange
array[price][0]:55.00
array[price][1]:41.13
array[tips][0]:red apple
array[tips][1]:4orange

大佬有話說 (9)

  • 資深大佬 : Pastsong

    这。。for-loop 不就好了

  • 資深大佬 : l1nyanm1ng

    “`js
    const array = [
    {
    name: ‘apple’,
    price: 55.0,
    tips: ‘red apple’,
    },
    {
    name: ‘orange’,
    price: 41.13,
    tips: ‘4 orange’,
    }
    ];

    const ret = array.reduce((accmulator, current) => {
    Object.entries(current).forEach(([prop, value]) => {
    if (!Reflect.has(accmulator, prop)) {
    accmulator[prop] = [];
    }
    accmulator[prop].push(value);
    });
    return accmulator;
    }, {});

    console.log(ret);
    “`

  • 資深大佬 : xiaoming1992

    跟#2 差不多
    “` javascript
    const arr = [
    {
    name: “name1”,
    price: “price1”,
    tips: “tips1”,
    },
    {
    name: “name2”,
    price: “price2”,
    tips: “tips2”,
    },
    ]

    const result = arr.reduce((prev, cur, i) => {
    Object.entries(cur).forEach(([key, val]) => {
    if (!prev[key]) { prev[key] = […new Array(arr.length)] }
    prev[key][i] = val
    })
    return prev
    }, {})

    console.log(result)
    “`

  • 資深大佬 : palmers

    “`js
    const arr = [{name: ‘apple’, price: 55.00, tips: ‘red apple’}, {name: ‘org’, price: 52.00, tips: ‘red apple’}];
    const vx = arr.reduce((ac, item) => {
    const keys = Object.keys(item);
    keys.map(key => {
    const has = ac.hasOwnProperty(key);
    if(has) {//{name: ‘apple’, price: 55.00, tips: ‘red apple’}
    const maybeArr = ac[key];
    const isArr = Array.isArray(maybeArr);
    if(isArr) { ac[key].push(item[key])}else {ac[key] = [ac[key], item[key]];}
    }else {
    ac[key] = [ac[key]];
    }
    });
    return ac;
    });
    console.log(vx);
    “`
    我这是比较笨拙的写法

  • 資深大佬 : jianguiqubaa

    arr.reduce((acc, item) => Object.entries(item).reduce((pre, [key, value]) => ({…pre, [key]: […(pre[key] || []), value]}), acc), {})

  • 資深大佬 : palmers

    @jianguiqubaa 完美 , 代码太帅了!!

  • 資深大佬 : kyuuseiryuu

    const arr = [
    ….{
    ……..name: “name1”,
    ……..price: “price1”,
    ……..tips: “tips1”,
    ….},
    ….{
    ……..name: “name2”,
    ……..price: “price2”,
    ……..tips: “tips2”,
    ….},
    ]
    console.log(arr);
    const result = {};
    arr.forEach(element => {
    ….Object.keys(element).forEach(key => {
    ……..if (!result[key]) {
    …………result[key] = [];
    ……..}
    ……..result[key].push(element[key]);
    ….})
    });

    console.log(result);

    为什么要用 reduce,foreach 不香吗?

  • 資深大佬 : onfuns

    for 循环啊,非开源项目不要写的太精简,不然后期有维护的可能性,到时就蛋疼了。业务代码就要一目了然!

  • 資深大佬 : clare233

    7 的优雅,业务代码确实不是写的越短约好,7 这个很一目了然

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具