Combine two json sections into one json object
You can use Object.assign()
to concatenate your objects.
var newObj = Object.assign({}, topData, bottomData)
From MDN:
The
Object.assign()
method is used to copy the values of all
enumerable own properties from one or more source objects to a target
object. It will return the target object.
var topData = { "auth": "1vmPoG22V3qqf43mPeMc", "property" : "ATL-D406", "status" : 1, "user" : "test001@aaa.com", "name" : "Abraham Denson"}
var bottomData = { "agent" : "pusher@agent.com", "agency" : "Thai Tims Agency", "agentCommission" : 1000, "arrival" : "arrive 12pm at condo", "departure" : "leaving room at 6pm",}
var completeData = Object.assign({}, topData, bottomData);
console.log(completeData);
Merge two JSON into one
It looks like you're actually dealing with JavaScript objects rather than JSON. This can be a bit confusing in JavaScript. You can kind of treat them in a similar way as they're easy to move between the two representations. The important thing as Krzysztof Krzeszewski mentioned in the comments is that JSON is a text format for data exchange.
The following is valid JSON:
{"key": "val"}
It also happens to be a valid JavaScript object as well. As a result when sending JSON over a HTTP request we will often transform the response into a JavaScript object to make it easy to work with on the frontend (or backend if you're using nodejs).
In this case it looks like that's being done for you, so you've actually got two JavaScript objects you wish to merge together. This can be done with Object.assign. For example:
let objOne = { keyOne: "val1" };
let objTwo = { keyTwo: "val2" };
let result = {};
Object.assigns(result, objOne, objTwo);
console.log(result);
// { keyOne: "val1", keyTwo: "val2" }
This takes a target object, and then multiple source objects and assigns the properties to the target object. It does this in the order you hand the targets to the function, thism eans if objTwo had a key called "keyOne" with a value of "val3" you'd end up with the following:
{ keyOne: "val3", keyTwo: "val2" }
It's useful to note that this isn't a "deep" merge. It will only do the top level properties. If you want to do a deep merge you'll either need to loop through the relevant object, or look at using a function from a library such as the merge function from lodash.
Combine two nested json objects with javascript and loop through it with a key
Just concat the arrays:
const obj1 = {
"countries": [{
"Country name": "China",
"Flag": "CN",
"Population": 1395380000,
"undefined": "#688144"
}
]
};
const obj2 = {
"countries": [{
"Country name": "India",
"Flag": "IN",
"Population": 1338677000,
"undefined": "#B78A31"
}
]
};
const result = {
countries: [...obj1.countries, ...obj2.countries]
};
console.log(result);
Merge two json together into one json in ngOnInit function
I think you can use Promise
ngOnInit() {
let dataPromise = new Promise((resolve) => {
this._classService.GetClassData()
.subscribe((result: any) => {
resolve(result[0]);
})
});
let timesPromise = new Promise((resolve) => {
this._classService.GetClassTimes()
.subscribe((result: any) => {
resolve(result[0]);
})
});
Promise.all([dataPromise, timesPromise])
.then((values) => {
console.log(values);
let completeData = { ...values[0], ...values[1]};
// let completeData = Object.assign({}, values[0], values[1]);
console.log("final results : ", completeData);
});
}
Related Topics
Dynamically Add Variable Name Value Pairs to Json Object
Regular Expression to Check Only One Decimal Point
React-Router Urls Don't Work When Refreshing or Writing Manually
Save State When Refresh Page - Reactjs
Correct Way to Push into State Array
How to Add a Dynamic Action That Will Redirect to Another Page by Executing JavaScript Code
How to Remove the Div That a Button Is Contained in When the Button Is Clicked
How to Merge Two Json Object Values by Id With Plain JavaScript (Es6)
Check If Page Gets Reloaded or Refreshed in JavaScript
How to Display a Success Message After Redirecting the User Back to My Home Page
How to Print Star Pattern in JavaScript in a Very Simple Manner
How to Clear Browsing History Using JavaScript
How to Update a Variable After Button Click
Force Download a Pdf Link Using Javascript/Ajax/Jquery