Merge two JSON objects containing arrays using jQuery
If you look at the documentation of jquery extend you can see that you can pass deep
as first argument:
var obj = $.extend(true, {},json1,json2);
With your test data, because the keys in the objects are named the same, the result will just have the second set of objects.
If you want some other result you need to update your question to clarify the desired effect.
Update
If you want the entries to be combined you can do something like this:
var entries = json1.entry.concat(json2.entry);
console.log(entries);
Javascript: Merge List of JSON Objects on Same Key and Drop Un-merged Objects
You can use es6 spread (...
) operator to marge two objects.
let arrObjA = [{ "index": 114, "realName": 'kevin', "bucket": 'boss', "react_name": 'BossKevin' }, { "index": 115, "realName": 'angela', "bucket": 'boss', "react_name": 'BossAngela' }, { "index": 116, "realName": 'james', "bucket": 'janitor', "react_name": 'JanitorJames' }, { "index": 117, "realName": 'arthur', "bucket": 'employee', "react_name": 'EmployeeArthur' }]
let arrObjB = [{ "boxName": "building", "realName": "angela", "boxValue": "2" }, { "boxName": "building", "realName": "james", "boxValue": "false" }, { "boxName": "building", "realName": "arthur", "boxValue": "0" },]
let result = arrObjB.map(item => ({
...arrObjA.find(({ realName }) => item.realName == realName),
...item,
}));
console.log(result)
How to merge multiple object arrays into one array cross with sub array?
You could iterate over the two arrays like this and push the merged values into result
:
arr1.forEach(e => {
arr2.forEach(e2 => {
result.push(Object.assign({}, e, e2));
});
});
Which could also be written in ONE LINE:
arr1.forEach(e => arr2.forEach(e2 => result.push(Object.assign({}, e, e2))));
const arr1 = [{ "userId": 9 }, { "userId": 14 }];
const arr2 = [{ "role": "1", "group": "3"}, { "role": "1", "group": "2"}];
const result = [];
arr1.forEach(e => { arr2.forEach(e2 => { result.push(Object.assign({}, e, e2)); });});
console.log(result);
How to join two JSON Array objects in Node
i already got an answer from the link provided by Pravin
var merge = function() {
var destination = {},
sources = [].slice.call( arguments, 0 );
sources.forEach(function( source ) {
var prop;
for ( prop in source ) {
if ( prop in destination && Array.isArray( destination[ prop ] ) ) {
// Concat Arrays
destination[ prop ] = destination[ prop ].concat( source[ prop ] );
} else if ( prop in destination && typeof destination[ prop ] === "object" ) {
// Merge Objects
destination[ prop ] = merge( destination[ prop ], source[ prop ] );
} else {
// Set new values
destination[ prop ] = source[ prop ];
}
}
});
return destination;
};
console.log(JSON.stringify(merge({ a: { b: 1, c: 2 } }, { a: { b: 3, d: 4 } })));
Merge two json array into one
There are some missing pieces there, but I think I see what's happening.
Based on the result you're getting, it looks like $posts
and $post_image
in this code are eloquent models.
return $this->successResponse($posts.$post_image );
When you concatenate them, their __toString()
methods convert them to strings, which is done using the toJson()
method. So basically you have two JSON objects stuck together, which isn't valid JSON, and then the successResponse()
method encodes them again.
To merge them, you can convert them to arrays, merge those, then pass the result to successResponse()
.
$merged = array_merge($posts->toArray(), $post_image->toArray());
return $this->successResponse($merged);
The result you want is impossible, though. The "post" object has two different values of "id". You'll only be able to get one. If you use
$merged = array_merge($posts->toArray(), $post_image->toArray());
Then the id value of the second object will replace the first one. If you want to keep the first id value, you need to use union instead of array_merge
.
$merged = $a->toArray() + $b->toArray();
Related Topics
Php - Return Confirm Within PHP Issue
Sum Numbers in HTML Inputs, JavaScript
How to Include a JavaScript in a Jsp Page
415 (Unsupported Media Type) With Rest Post Request
Display Image from Http Response With Image Content Type
How to Check If a Background Image Is Loaded
How to Make Any Hovered Color Darker/Lighter That the Original
Prevent JavaScript String That Contains a Number from Dropping Zeros
React History.Push() Not Rendering New Component
Reinitialize/Reset Dropzone After Form Is Submitted
Getting Typeerror: This.Props Is Undefined on Reactjs
Validate Phone Number With JavaScript
Removing Currency Symbol and Replacing Comma With Point Using Pure JavaScript
Angular 2 Not Updating Until Any Object Is Clicked
What Is the Max Size of Localstorage Values
Why Isnt Window.Location.Href= Not Forwarding to Page Using Safari
How to Set Profile Image as First Letters of First and Last Name