根据JSON的值和JSON数组键合并一个JSON和JSON数组

时间:2016-11-23 07:30:30

标签: javascript json node.js

我想通过组合这两个JSON和JSON数组来尝试在节点中创建一个JSON数组

`var template = {
    "key1": "value1",
    "key3": "value3",
    "key4": "value3",
    "key6": "Dummy Value1"
};
var data = [
  {
    "value1": "1",
    "value2": "2",
    "value3": "3"
  },
  {
    "value1": "11",
    "value2": "12",
    "value3": "13"
  },
  {
    "value1": "21",
    "value2": "22",
    "value3": "23"
  }
];`

我需要结果JSON就像

var result = [
  {
    "key1": "1",
    "key3": "3",
    "key4": "3",
    "key6": "Dummy Value1"
  },
  {
    "key1": "11",
    "key3": "13",
    "key4": "13",
    "key6": "Dummy Value1"
  },
  {
    "key1": "21",
    "key3": "23",
    "key4": "23",
    "key6": "Dummy Value1"
  }
]

在我的情况下,JSON数组中将有大约40个键,模板JSON中将有大约25个键,因此我也在寻找优化解决方案。有人可以帮我吗?

3 个答案:

答案 0 :(得分:2)

使用Array#mapArray#reduce方法。

// iterate over the array
var res = data.map(function(o) {
  // get the property names array and iterate to generate 
  // result object
  return Object.keys(template).reduce(function(obj, k) {
    // define property based on the array element object has 
    // the prefered property or not
    obj[k] = o.hasOwnProperty(template[k]) ? o[template[k]] : template[k];
    // return the object reference
    return obj;
    // set initial value as an empty object which is used as
    // the new array element 
  }, {});
});

var template = {
  "key1": "value1",
  "key3": "value3",
  "key4": "value3",
  "key6": "Dummy Value1"
};
var data = [{
  "value1": "1",
  "value2": "2",
  "value3": "3"
}, {
  "value1": "11",
  "value2": "12",
  "value3": "13"
}, {
  "value1": "21",
  "value2": "22",
  "value3": "23"
}];

var res = data.map(function(o) {
  return Object.keys(template).reduce(function(obj, k) {
    obj[k] = o.hasOwnProperty(template[k]) ? o[template[k]] : template[k];
    return obj;
  }, {});
});

console.log(res);

答案 1 :(得分:1)

您可以迭代给定的数据数组和键,并将结果映射到新数组。

var template = { "key1": "value1", "key3": "value3", "key4": "value3", "key6": "Dummy Value1" },
    data = [{ "value1": "1", "value2": "2", "value3": "3" }, { "value1": "11", "value2": "12", "value3": "13" }, { "value1": "21", "value2": "22", "value3": "23" }],
    result = data.map(function (a) {
        var o = {};
        this.forEach(function (k) {
            o[k] = template[k] in a ? a[template[k]] : template[k];
        })
        return o;
    }, Object.keys(template));
	
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

ES6

var template = { "key1": "value1", "key3": "value3", "key4": "value3", "key6": "Dummy Value1" },
    data = [{ "value1": "1", "value2": "2", "value3": "3" }, { "value1": "11", "value2": "12", "value3": "13" }, { "value1": "21", "value2": "22", "value3": "23" }],
    keys = Object.keys(template),
    result = data.map(a =>
        keys.reduce((o, k) => (t => Object.assign(o, { [k]: t in a ? a[t] : t }))(template[k]), {}));
	
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:1)

您可以使用array map执行此操作。

var template = {
  "key1": "value1",
  "key3": "value3",
  "key4": "value3",
  "key6": "Dummy Value1"
};
var data = [{
  "value1": "1",
  "value2": "2",
  "value3": "3"
}, {
  "value1": "11",
  "value2": "12",
  "value3": "13"
}, {
  "value1": "21",
  "value2": "22",
  "value3": "23"
}];

result = data.map(function(i){
  var o = {};
  for (var key in template){
    o[key] = template[key] in i ? i[template[key]] : template[key];
    }
  return o;
});

console.log(result);
``