I am using Nodejs. I need to store a JavaScript object in a relational database. Each key in the JavaScript object represents column name in DB. I have following:

我正在使用Nodejs。我需要在关系数据库中存储JavaScript对象。 JavaScript对象中的每个键代表DB中的列名。我有以下内容:

var data = {
  "aa": "99",
  "bb": ["11","22"],
  "cc": ["44","55","66"]
}

I want to convert this object into an array as follow:

我想将此对象转换为数组,如下所示:

  data =  [
    {
    "aa": "99",
    "bb": "11",
    "cc": "44"
    },
    {
    "aa": "99",
    "bb": "11",
    "cc": "55"
    },
    {
    "aa": "99",
    "bb": "11",
    "cc": "66"
    },
    {
    "aa": "99",
    "bb": "22",
    "cc": "44"
    },
    {
    "aa": "99",
    "bb": "22",
    "cc": "55"
    },
    {
    "aa": "99",
    "bb": "22",
    "cc": "66"
    }
    ]

Is there any way to do this ? I guess using recursive we can do it. But could not find any reference in Google.

有没有办法做到这一点?我想使用递归我们可以做到。但在谷歌找不到任何参考。

2 个解决方案

#1


1

You could use an iterative and recursive approach with a combination algorithm.

您可以使用组合算法的迭代和递归方法。

This solution dies basically iterate through the given data (an array is made out of the object) and inside of the array iterated over the items. In this case, you have an array with three arrays with the items.

这个解决方案基本上遍历给定的数据(一个数组由对象组成),并在数组内部迭代遍历项目。在这种情况下,您有一个包含三个数组的数组。

[
    ["99"],
    ["11","22"],
    ["44","55","66"]
]

It starts with the first array and iterates. Here is only one item and the callback of the iteraton check for the part length and if it is equal to the given array length, al item are acually found. This is the cndition to exit the iteration adn to push the collected parts to the result array. (The items are converted to an object in the reduce callback.)

它从第一个数组开始并迭代。这里只有一个项目和iteraton的回调检查部件长度,如果它等于给定的数组长度,则可以找到al item。这是退出迭代adn以将收集的部分推送到结果数组的结果。 (这些项目将转换为reduce回调中的对象。)

If the part array does not have the needed length, proceed with the next item of the outer array.

如果零件数组没有所需的长度,请继续执行外部数组的下一项。

Basically the iteration and recusion works as follow

基本上迭代和重复的工作如下

part       0      1      2   action
       ------  -----  -----  ---------------
          99                 go to next level
          99     11          go to next level
          99     11     44   push to result, end level 2
          99     11     55   push to result, end level 2
          99     11     66   push to result, end level 2
          99     11          end level 1
          99     22          go to next level
          99     22     44   push to result, end level 2
          99     22     55   push to result, end level 2
          99     22     66   push to result, end level 2
          99     22          end level 1
          99                 end level 0

function combine(object) {
    function c(part) {
        array[part.length].forEach(function (a) {
            var p = part.concat(a);
            if (p.length === array.length) {
                result.push(p.reduce(function (r, b, i) {
                    r[keys[i]] = b;
                    return r;
                }, {}));
                return;
            }
            c(p);
        });
    }

    var keys = Object.keys(object),
        array = keys.map(function (k) { return Array.isArray(object[k]) ? object[k] : [object[k]]; }),
        result = [];

    c([]);
    return result;
}

var data = { aa: "99", bb: ["11", "22"], cc: ["44", "55", "66"] },
    result = combine(data);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

更多相关文章

  1. 显示json数组中的所有项目
  2. 图表。js数据数组使用PHP, MySQL。如何从JSON数组定义数据源?
  3. javascript数组和对象是否有设置顺序?
  4. 从另一个数组中删除数组的内容。
  5. 从两个数组生成JSON
  6. 如何将图像(PNG)转换为2D数组(二进制图像)?
  7. JavaScript循环输入创建一个对象数组
  8. 有没有办法检查两个数组是否具有相同的元素?
  9. js中数组的使用方法

随机推荐

  1. Android Animation学习
  2. Android Afinal使用与总结
  3. Android入门 — 模拟器的创建和运行
  4. android 开发
  5. MobSF安装使用及过程中遇到的错误
  6. Android消息推送实现
  7. Weex 04 Weex中Android项目的生成和交互
  8. android TextView属性大全(转)
  9. Android(安卓)如何在代码中动态的添加Vie
  10. android设备连接到pc进行应用程序调试