小编Kle*_*sas的帖子

使用lodash将数组转换为对象

我需要将数组或数组数组转换为具有从名称数组命名的键​​的对象.例:

//given names
    names = ['first', 'second', 'third', 'fourth'] 
//array
    param = [1, 2, 3, 4] 
//becomes 
    result = {first: 1, second: 2, third: 3, fourth: 4}

//array of arrays 
    param = [
      [1, 2, 3, 4],
      [-4, 3, 1, 32],
    ]
//becomes
    result = [
      {first: 1, second: 2, third: 3, fourth: 4},
      {first: -4, second: 3, third: 1, fourth: 32},
    ]
Run Code Online (Sandbox Code Playgroud)

我目前的解决方案是:

    var names = ['first', 'second', 'third', 'forth'];

    function arrayToObject(array, names) {
      var result = {};
      for …
Run Code Online (Sandbox Code Playgroud)

javascript arrays object lodash

2
推荐指数
2
解决办法
7047
查看次数

标签 统计

arrays ×1

javascript ×1

lodash ×1

object ×1