2

I wanna try to create one JSON array in node by combine these two JSON and JSON arrays

`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"
  }
];`

I need Resulted JSON be like

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"
  }
]

In my case there will be around 40 keys in JSON array and around 25 keys in template JSON, So I'm also looking for Optimized solution for this. Anybody can help me in this?

3
  • Where are the data inside the data array ? Why is there key1 : 1, key3 : 3, key4 :3 everywhere ? You just want to replace every data with the template ? Commented Nov 23, 2016 at 7:33
  • 1
    you sure. i think you want result like 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" } ]? Commented Nov 23, 2016 at 7:33
  • Sorry,Yes ashish you are right.. i need to update my result JSON Commented Nov 23, 2016 at 7:35

3 Answers 3

2

Use Array#map and Array#reduce methods.

// 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);

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Pranav C Balan :)
@MayankJain : glad to help you :)
1

You can use array map to do this.

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);
``

Comments

1

You could iterate the given data array and the keys and map the result for a new array.

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; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.