将多个键和值合并到多个对象

时间:2019-06-20 12:41:20

标签: node.js typescript

这是我的请求正文:

{
  "data": [{
    "firstName": "a"
  }, {
    "lastName": "b"
  }, ...]
}

这包括我的脚本:

export default class SecureObjects {
  public create(key: any, value: any) {
    var result = {};
    for (var i = 0; i < key.length; i++) {
      result[key[i]] = value[i];
    }
    return result;
  }
}

这是我的脚本:

var body = request.body;
var key = body.data.map(item => Object.keys(item)).flat();
var value = body.data.map(item => Object.values(item)).flat();
var secureObjects = new SecureObjects();
var r =  secureObjects.create(key, value);
console.log(r);

响应看起来像这样{ firstName: 'Achmad', lastName: 'a' },但我希望它看起来像这样{ firstName: 'Achmad'},{lastName: 'a' }, ...

谢谢。

0 个答案:

没有答案