Adhikar Patil

Adhikar Patil

  • NA
  • 481
  • 127.6k

How to combine json's in angular 8

Mar 15 2020 12:16 PM
Hello,
I have three json into array like and also i want array[0] with the first json including other jsons nonmatching key with blank value. Please provide me the solution in angular 8
json1 = {name: "Adhikar", email: "[email protected]", phoneNumber: "(898) 990-9090", cropType: "rawCrop", cropName: {jawar: true, bajri: false}, cropPeriod: 120}
json2 = {name: "Sagar", email: "[email protected]", phoneNumber: "", cropType: "", phvalue: 90 }
json3 = {name: "Ajay", email: "[email protected]", phoneNumber: "", cropType: "rawCrop", cropWeather: "winter"}
so i want to update my array[0] Like this
json1 = {name: "Adhikar", email: "[email protected]", phoneNumber: "(898) 990-9090", cropType: "rawCrop", cropName: {jawar: true, bajri: false}, cropPeriod: 120, phvalue:"", cropWeather: ""}
json2 = {name: "Sagar", email: "[email protected]", phoneNumber: "", cropType: "", phvalue: 90 }
json3 = {name: "Ajay", email: "[email protected]", phoneNumber: "", cropType: "rawCrop", cropWeather: "winter"}
 
 
Here is my code, please see the code 
 
 
getDynamicFormData() {
this.dynamicFormResponseService.get(this.id).subscribe(
res => {
if (res && res != null) {
this.dynamicFormData = res;
const array = [];
const ids = [];
this.dynamicFormData.forEach(function (item) {
if (array[0] == null) {
array.push(JSON.parse(item.response));
}
else {
var mainArray = JSON.stringify(array[0]);
for (let key in JSON.parse(mainArray)) {
if (JSON.parse(mainArray).hasOwnProperty(key) == JSON.parse(item.response).hasOwnProperty(key)) {
}
}
console.log(array);
array.push(JSON.parse(item.response));
ids.push(item.id);
}
});
this.dynamicFormResponses = array;
this.dynamicFormResponsesId = ids;
}
},
error => {
this.toastr.error(error.message);
}
);
}
 

Answers (3)