V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
LionXen
V2EX  ›  JavaScript

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

  •  
  •   LionXen · 2020-01-03 01:00:51 +08:00 · 2681 次点击
    这是一个创建于 1547 天前的主题,其中的信息可能已经有所发展或是发生改变。
    请教大佬。

    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 条回复    2020-01-03 23:49:44 +08:00
    Pastsong
        1
    Pastsong  
       2020-01-03 01:17:05 +08:00
    这。。for-loop 不就好了
    l1nyanm1ng
        2
    l1nyanm1ng  
       2020-01-03 09:28:35 +08:00   ❤️ 1
    ```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
        3
    xiaoming1992  
       2020-01-03 09:47:56 +08:00   ❤️ 1
    跟#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
        4
    palmers  
       2020-01-03 10:48:45 +08:00   ❤️ 1
    ```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);
    ```
    我这是比较笨拙的写法 😄 基本都是 reduce 来做
    jianguiqubaa
        5
    jianguiqubaa  
       2020-01-03 10:50:03 +08:00   ❤️ 5
    arr.reduce((acc, item) => Object.entries(item).reduce((pre, [key, value]) => ({...pre, [key]: [...(pre[key] || []), value]}), acc), {})
    palmers
        6
    palmers  
       2020-01-03 11:01:04 +08:00
    @jianguiqubaa 完美 , 代码太帅了!!
    kyuuseiryuu
        7
    kyuuseiryuu  
       2020-01-03 14:09:32 +08:00   ❤️ 3
    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
        8
    onfuns  
       2020-01-03 17:04:44 +08:00   ❤️ 1
    for 循环啊,非开源项目不要写的太精简,不然后期有维护的可能性,到时就蛋疼了。业务代码就要一目了然!
    clare233
        9
    clare233  
       2020-01-03 23:49:44 +08:00
    7 楼的优雅,业务代码确实不是写的越短约好,7 楼这个很一目了然
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   948 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:57 · PVG 04:57 · LAX 13:57 · JFK 16:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.