使用lodash转换嵌套对象

时间:2019-03-23 13:52:07

标签: javascript lodash

输入:

#include "stm32f10x.h" // Device header #include "delay.h"

void pinConfig(void);

void i2c_Master_Config(void);

void sendData(uint8_t data);

int main() {

    delay_init();
    pinConfig();
    i2c_Master_Config();

    while(1)
    {
        uint8_t butonState=GPIOA->IDR & 0x00001000; 
        sendData(0x68,butonState);
        delay_ms(10);
    }
}

void pinConfig() {

    RCC->APB1ENR |=1<<21;//Enable I2C 1 clock
    RCC->APB2ENR   |=1<<2;//Enable GPIOA clock
    RCC->APB2ENR   |=1<<3;//Enable GPIOB clock 
    RCC->APB2ENR   |=1<<0;//Enable AFIO clock

    GPIOA->CRL |= 0x00008000; //PA3 button pull-down 
    GPIOB->CRL = 0xFF000000; //SCL and SDA  AF Open Drain  SCL => PB6  SDA     =>PB7
}

void i2c_Master_Config() {

    I2C1->CR2    |=1<<5; //36 Mhz peripheral clock.
    I2C1->CR2    |=1<<2; //36 Mhz peripheral clock.
    I2C1->CCR     =0x28;//100 khz clock  
    I2C1->TRISE   =9;//1/8MHZ= 125 ns  => 1000ns/125ns =8  => 8+1 =9
    I2C1->CR1     |=(1<<0);//Peripheral enable..
}

void sendData(uint8_t data) {
    volatile int temp;

    while(I2C1->SR2 &(1<<1));//BUSY bit.
    I2C1->CR1  |=1<<8;//START bit.
    while(!(I2C1->SR1 & (1<<0))); //wait until start flag is set

    I2C1->DR  = slaveAdres<<1;//7 bit adress.

    while(!(I2C1->SR1 &(1<<1)));//wait until addr flag is set
    gecici=I2C1->SR2;//clear addr flag.

    I2C1->DR = data; 
    while (!(I2C1->SR1 & (1<<7))){} //wait until txe is set

    while (!(I2C1->SR1 & (1<<2)));//BTF(Byte transfer finished)=1 . 
    I2C1->CR1 |= 1<<9;//STOP bit.
    I2C1->CR1     &=~(1<<0);//Peripheral disable.
}

输出:

#include "stm32f10x.h" // Device header

void pinConfig(void);

void i2c_Slave_Config(void);

uint8_t readData(void);

uint8_t data;

int main()
{

    pinConfig();
    i2c_Slave_Config();

    while(1)
    {
        data=readData();

        if(data==0)
            GPIOB->BSRR |=1<<3;
        else if(data==1)
            GPIOB->BRR  |=1<<3;
    }
}

void pinConfig()
{

    RCC->APB1ENR |=1<<21;//I2C 1 Clock Aktif.
    RCC->APB2ENR   |=1<<2;//Enable GPIOA clock
    RCC->APB2ENR   |=1<<3;//Enable GPIOB clock 
    RCC->APB2ENR   |=1<<0;//Enable AFIO clock

    GPIOA->CRL |= 0x00002000; //PA3 led.
    GPIOB->CRL = 0xFF000000; //SCL and SDA  AF Open Drain  SCL => PB6  SDA   =>PB7
    GPIOA->BSRR |=1<<3;//Turn off the led.
}

void i2c_Slave_Config() {

    RCC->APB1ENR |=1<<21;//I2C 1 Clock Enable.
    I2C1->CR2    |=1<<5; //36 Mhz peripheral clock.
    I2C1->CR2    |=1<<2; //36 Mhz peripheral clock.
    I2C1->CCR     =0x28;//100 khz clock  
    I2C1->OAR1    &=~(1<<15);//7-bit slave adress.
    I2C1 ->CR1     |= 1<<10;//ACK enable.

    //0x68 Slave Adress Configured.    
    I2C1 ->OAR1   &=~(1<<1);
    I2C1 ->OAR1   &=~(1<<2);
    I2C1 ->OAR1   &=~(1<<3);
    I2C1 ->OAR1   &=~(1<<5);
    I2C1 ->OAR1   |=(1<<4);
    I2C1 ->OAR1   |=(1<<6);
    I2C1 ->OAR1   |=(1<<7);
    //0x68 Slave Adress Configured.
}

uint8_t readData()
{
    volatile int temp;
    uint8_t data;

    I2C1->CR1     |=(1<<0);//Peripheral enable.

    while(I2C1->SR2 &(1<<1));//BUSY bit.
    I2C1->CR1  |=1<<8;//START bit.
    while(!(I2C1->SR1 & (1<<0))); // wait until start flag is set.

    while(!(I2C1->SR1 &(1<<1)));// wait until addr flag is set
    temp=I2C1->SR2;//clear addr .

    while (!(I2C1->SR1 & (1<<6))){} // wait until rxne is set
    data=I2C1->DR;

    while (!(I2C1->SR1 & (1<<4))){} // wait until STOPF is set
    gecici=I2C1->SR1;

    I2C1->SR1 |=1<<9;

    I2C1->CR1     &=~(1<<0);//Peripheral disable.
    return data;
}

我尝试了以下效果很好的解决方案,但需要知道是否有使用lodash和JS进行此操作的更好方法。

const a =  {
            "8": [{
                "strategy": 123,
                "id": 1,
                "config": {
                    "global_dag_conf": {
                        "algo_v2_conf": {
                            "features_to_combine": [],
                            "segments": [],
                            "force_performance": false,
                            "min_bid": 0,
                            "max_bid": 13
                        }
                    }
                }
            }],
            "13": [{
                "strategy": 456,
                "id": 2,
                "config": {
                    "global_dag_conf": {
                        "algo_v2_conf": {
                            "ivr_measured": []
                        }
                    }
                }
            }]

        }

4 个答案:

答案 0 :(得分:1)

使用_.mapValues()来迭代密钥,并使用Array.map()object destructuringspread syntax来重新格式化对象:

Drop
const data = {"8":[{"strategy":123,"id":1,"config":{"global_dag_conf":{"algo_v2_conf":{"features_to_combine":[],"segments":[],"force_performance":false,"min_bid":0,"max_bid":13}}}}],"13":[{"strategy":456,"id":2,"config":{"global_dag_conf":{"algo_v2_conf":{"ivr_measured":[]}}}}]}

const result = _.mapValues(data, 
  arr => arr.map(({ id: algo_id, config }) => 
    ({ algo_id, ...config })
))

console.log(result)

答案 1 :(得分:1)

这是不使用lodash的解决方案:

  • 使用Object.entries()来获取键值对数组
  • 通过在数组上使用reduce创建新对象
  • 使用map创建一个新的对象数组。
  • 解构每个对象以获得idconfigSpread config变量可删除一级嵌套

const input = {"8":[{"strategy":123,"id":1,"config":{"global_dag_conf":{"algo_v2_conf":{"features_to_combine":[],"segments":[],"force_performance":false,"min_bid":0,"max_bid":13}}}}],"13":[{"strategy":456,"id":2,"config":{"global_dag_conf":{"algo_v2_conf":{"ivr_measured":[]}}}}]}

const output = 

Object.entries(input)
      .reduce((r, [key, value]) => {
          r[key] = value.map(({ id, config }) => ({ algo_id: id, ...config }));
          return r;
      }, {})
      
console.log(output)

答案 2 :(得分:1)

使用mapValuesmapassign方法的纯Lodash解决方案

let data = {"8":[{"strategy":123,"id":1,"config":{"global_dag_conf":{"algo_v2_conf":{"features_to_combine":[],"segments":[],"force_performance":false,"min_bid":0,"max_bid":13}}}}],"13":[{"strategy":456,"id":2,"config":{"global_dag_conf":{"algo_v2_conf":{"ivr_measured":[]}}}}]};

let res = _.mapValues(data, arr => _.map(arr, obj => _.assign({
  'algo_id': obj.id,
  'global_dag_conf': obj.config.global_dag_conf
})));

console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

答案 3 :(得分:0)

没有lodash的替代方法。

函数reduce允许生成一个对象,该对象将使用函数map填充,该函数将原始对象转换为所需的结构。

const a =  {            "8": [{                "strategy": 123,                "id": 1,                "config": {                    "global_dag_conf": {                        "algo_v2_conf": {                            "features_to_combine": [],                            "segments": [],                            "force_performance": false,                            "min_bid": 0,                            "max_bid": 13                        }                    }                }            }],            "13": [{                "strategy": 456,                "id": 2,                "config": {                    "global_dag_conf": {                        "algo_v2_conf": {                            "ivr_measured": []                        }                    }                }            }]        };
        
let result = Object.entries(a).reduce((a, [key, arr]) => {
  return Object.assign(a, {[key]: arr.map(({id: algo_id, config}) => ({algo_id, ...config}))});
}, Object.create(null));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }