构建基于key& amp;的数组对象

时间:2016-09-28 13:58:13

标签: javascript node.js excel

我正在尝试创建一个接收这样输入的代码:

{
  "11": {
    "08/2011": 1
  },
  "12": {
    "08/2011": 2
  },
  "13": {
    "08/2011": 3
  }
}

并输出如下数组:

Excel => [
          [ ""         , "11" , "12" , "13" ],  <- headers
          [ "08/2011"  ,  1   , 2    , 3    ]   <- first line
                                              ]

此处的目标是将输出传递给node-xlsx,以便创建Excel电子表格。

我能够创建一个模型:

let headers = ['', ];
let data = [headers];
let body = [];

let papas = {
    "11": {
    	"08/2011": 1
    },
    "12": {
    	"08/2011": 2
    },
    "13": {
    	"08/2011": 3
    }
};

for (let cnpjs in papas) {
	headers.push(cnpjs);
}

let x = {};

for (let cnpj in papas) {
	Object.keys(papas[cnpj]).forEach((lasanha, index) => {
		if (typeof x[lasanha] !== 'number') {
			x[lasanha] = index;
			data.push(body);
			body.push(lasanha);
		} else {
			lasanha = lasanha;
		}
		body.push(papas[cnpj][lasanha]);
	});
}

document.body.innerHTML = JSON.stringify(data, null, 4);

问题是,输入如下:

{
  "11": {
    "08/2011": 1
    "09/2011": 1
  },
  "12": {
    "08/2011": 2
  },
  "13": {
    "08/2011": 3
  }
}

将弄乱逻辑,输出如下内容:

[
 [ ""        , "11" , "12"      , "13"         ],
 [ "08/2011" , 1    , "09/2011" , 1    ,  2, 3 ],
 [ "09/2011" , 1    , "09/2011" , 1    ,  2, 3 ] ]

什么时候应该:

 [
   [ ""         , "11" , "12" , "13" ],
   [ "08/2011"  ,  1   , 2    , 3    ],
   [ "09/2011"  ,  1                 ]
                                       ]

任何帮助将不胜感激。 感谢。

更新

不是吗,但它更接近......当我这样输入时:

{
  "11": {
    "08/2011": 1,
    "09/2011": 1
  },
  "12": {
    "08/2011": 2,
    "10/2011": 1
  },
  "13": {
    "08/2011": 3
  }
}

输出应为:

[ [ ''         , '11' , '12' , '13' ],
  [ '08/2011'  , 1    , 2    , 3    ],
  [ '09/2011'  , 1                  ],
  [ '10/2011'  ,      , 1           ] ]

10/2011在这种情况下属于12

2 个答案:

答案 0 :(得分:1)

您可以使用Object.keys()和两个forEach()循环执行此操作。注意:that在第一个循环中引用this,它是作为可选thisArg参数的对象。

&#13;
&#13;
var input = {
  "11": {
    "08/2011": 1,
    "09/2011": 1
  },
  "12": {
    "08/2011": 2,
    "10/2011": 1
  },
  "13": {
    "08/2011": 3
  }
}

var result = [];
Object.keys(input).forEach(function(k, i) {
  (i == 0) ? result.push(['', k]): result[0].push(k);
  var that = this;
  Object.keys(input[k]).forEach(function(e) {
    if (!that[e]) {
      that[e] = [e];
      result.push(that[e]);
    }
    that[e][i + 1] = input[k][e];
  })
}, {})

console.log(result)
&#13;
&#13;
&#13;

答案 1 :(得分:0)

试试这个:

&#13;
&#13;
// It was hard to write, it should be hard to understand.
const transform = input => [
  ['', ...Object.keys(input)]
 ,...[].concat(...Object.values(input).map(Object.keys))
    .filter((x, i, arr) => arr.indexOf(x) === i)
    .map(first => [first, ...Object.values(input).map(x =>
       Object.entries(x).find(([key, value]) => key === first)).map(x => x && x[1])])]

console.log(transform({
  "11": {
    "08/2011": 1,
    "09/2011": 1
  },
  "12": {
    "08/2011": 2,
    "10/2011": 1
  },
  "13": {
    "08/2011": 3
  }
}))
&#13;
&#13;
&#13;