If you want to map objects to something (in this case a property). I think Array.prototype.map is what you're looking for if you want to code functionally.

(fiddle)

If you want to support older browsers, that are not ES5 compliant you can shim it (there is a polyfill on the MDN page above). Another alternative would be to use underscorejs's pluck method:

var users = [
      {name: "Joe", age: 22},
      {name: "Kevin", age: 24},
      {name: "Peter", age: 21}
    ];
var result = _.pluck(users,'name').join(",")
Answer from Benjamin Gruenbaum on Stack Overflow
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ typescript โ€บ typescript_array_join.htm
TypeScript - Array join()
join() method joins all the elements of an array into a string. separator โˆ’ Specifies a string to separate each element of the array. If omitted, the array elements are separated with a comma.
๐ŸŒ
Codemzy
codemzy.com โ€บ blog โ€บ array-join-object-property
Using Array join() on an object property - Codemzy's Blog
May 30, 2023 - If you did want to join your array of objects, you can turn the objects into strings first using Array.map() on the array, and JSON.stringify() on the objects.
Discussions

Perform .join on value in array of objects
Despite the code being slightly less clean/clear. It is a much more efficient than piping the map function to a join. The reason for this is because Array.map has to loop over each element to return a new array with all of the names of the object in the array. More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to join fields of an array of objects in javascript/typescript - Stack Overflow
The join function of an array can only be used on the whole entry, not on fields of the object that is underneath. More on stackoverflow.com
๐ŸŒ stackoverflow.com
merge two object arrays with Angular 2 and TypeScript? - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I have gone across the JavaScript questions on this topic, this question is specifically about Angular2 with TypeScript. What I am trying to do is to concatenate the json objects to an array. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Join values of a field in a JSON array
I get following array from a API query in JSON { "category": [{ "id": 28, "name": "Dogs" }, { "id": 14, "name": "Cats" }, { "id": 878, "name": "Sheep" }] } The output I need is a joined list - Dogs,Cats,Sheep I tried figuring it out with Javascript code and found some codes using .map but ... More on community.glideapps.com
๐ŸŒ community.glideapps.com
1
0
August 24, 2023
๐ŸŒ
Reddit
reddit.com โ€บ r/typescript โ€บ help with handling json array of objects (merge two arrays)
r/typescript on Reddit: Help with handling JSON array of objects (merge two arrays)
April 23, 2020 -

I have a json array of Objects and need to pick a corresponding object in that array according to some calculations.

My json have a following structure: {"OD": number, "SCH" {"SCH40": number, "SCH80": number}}

I need to do some math with OD and SCH numbers, compare it with an outside number and find corresponding value for OD key.

I can get a new array (of numbers I compare the outside number with):

this.Pipeareas = this.Pipesizes.map (x => Math.pow((x.OD - 2 * x.SCH[this.selectedSCH])/2000,2) * Math.PI);

How to merge existing and new arrays?

I tried (this add extra existing array and new object to new array (not key:value pair))

this.Pipeareas = {...this.Pipesizes, Area: this.Pipesizes.map (x => Math.pow((x.OD - 2 * x.SCH[this.selectedSCH])/2000,2) * Math.PI) }

and (this adds new key "Area" to each object but the value is full array, not corresponding numbers from new array)

this.Pipesizes.map (x => x.Area = this.Pipesizes.map (x => Math.pow((x.OD - 2 * x.SCH[this.selectedSCH])/2000,2) * Math.PI))

The result should be like:

return this.Pipeareas.find(x => x >= this.OutsideNumber);

๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ join
Array.prototype.join() - JavaScript - MDN Web Docs
If the array has only one item, then that item will be returned without using the separator. const elements = ["Fire", "Air", "Water"]; console.log(elements.join()); // Expected output: "Fire,Air,Water" console.log(elements.join("")); // Expected output: "FireAirWater" console.log(elements.join("-")); // Expected output: "Fire-Air-Water" ... A string to separate each pair of adjacent elements of the array.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ typescript-array-join-method
TypeScript Array join() Method - GeeksforGeeks
July 12, 2024 - The Array.join() method in TypeScript is a built-in function used to join all the elements of an array into a single string.
Find elsewhere
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ javascript join the array of objects | example code
JavaScript join the array of objects | Example code
January 19, 2018 - Code snip for join object values. console.log([ {name: "Joe", age: 22}, {name: "Kevin", age: 24}, {name: "Peter", age: 21} ].map(function(elem){ return elem.name; }).join(",")); In modern JavaScript, this will not perform with the join method only on the name attribute, to achieve the same output as before. console.log([ {name: "Joe", age: 22}, {name: "Kevin", age: 24}, {name: "Peter", age: 21} ].map(e => e.name).join(","));
๐ŸŒ
DEV Community
dev.to โ€บ fullstackchris โ€บ advanced-typescript-a-generic-function-to-merge-object-arrays-18ja
Advanced TypeScript: A Generic Function to Merge Object Arrays - DEV Community
December 9, 2021 - You could explicitly write key / value assignments for the keys that you want to update... or you can leverage JavaScript's built in Object.assign function and TypeScript's generic capabilities to only write one such function for all merging actions you need across your entire app! ๐Ÿ˜„ ยท For example, in ReduxPlate, I have two types, IFile, and IEditorSettings: ... IEditorSettings extends IFile and has just one additional property: isActive. When visitors click the "Generate!" button on the MVP page, the response from the server returns an array of objects of type IFile instead of IEditorSettings, since the server is not concerned with the isActive property.
Top answer
1 of 10
123

You want the concat method.

var finalObj = json1.concat(json2);
2 of 10
39

Upon first appearance, the word "merg" leads one to think you need to use .extend, which is the proper jQuery way to "merge" JSON objects. However, $.extend(true, {}, json1, json2); will cause all values sharing the same key name to be overridden by the latest supplied in the params. As review of your question shows, this is undesired.

What you seek is a simple javascript function known as .concat. Which would work like:

var finalObj = json1.concat(json2);

While this is not a native jQuery function, you could easily add it to the jQuery library for simple future use as follows:

;(function($) {
    if (!$.concat) {
        $.extend({
            concat: function() {
                return Array.prototype.concat.apply([], arguments);
            }
        });
    }
})(jQuery);

And then recall it as desired like:

var finalObj = $.concat(json1, json2);

You can also use it for multiple array objects of this type with a like:

var finalObj = $.concat(json1, json2, json3, json4, json5, ....);

And if you really want it jQuery style and very short and sweet (aka minified)

;(function(a){a.concat||a.extend({concat:function(){return Array.prototype.concat.apply([],arguments);}})})(jQuery);

;(function($){$.concat||$.extend({concat:function(){return Array.prototype.concat.apply([],arguments);}})})(jQuery);

$(function() {
    var json1 = [{id:1, name: 'xxx'}],
        json2 = [{id:2, name: 'xyz'}],
        json3 = [{id:3, name: 'xyy'}],
        json4 = [{id:4, name: 'xzy'}],
        json5 = [{id:5, name: 'zxy'}];
    
    console.log(Array(10).join('-')+'(json1, json2, json3)'+Array(10).join('-'));
    console.log($.concat(json1, json2, json3));
    console.log(Array(10).join('-')+'(json1, json2, json3, json4, json5)'+Array(10).join('-'));
    console.log($.concat(json1, json2, json3, json4, json5));
    console.log(Array(10).join('-')+'(json4, json1, json2, json5)'+Array(10).join('-'));
    console.log($.concat(json4, json1, json2, json5));
});
center { padding: 3em; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<center>See Console Log</center>

jsFiddle

๐ŸŒ
techtutorialsx
techtutorialsx.wordpress.com โ€บ 2020 โ€บ 09 โ€บ 06 โ€บ javascript-merge-json-objects
JavaScript merge JSON objects โ€“ techtutorialsx
January 25, 2025 - We will assume that we are starting from two JSON strings and, at the end, we want to obtain a single JSON string with the merged objects. Nonetheless, we will do the merging operation over the parsed objects, on their JavaScript representation. Note however that the merging operation is useful outside the scope of JSON processing.
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_join.asp
JavaScript Array join() Method
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST ... Array[ ] Array( ) at() concat() constructor copyWithin() entries() every() fill() filter() find() findIndex() findLast() findLastIndex() flat() flatMap() forEach() from() includes() indexOf() isArray() join() keys() lastIndexOf() length map() of() pop() prototype push() reduce() reduceRight() rest (...) reverse() shift() slice() some() sort() splice() spread (...) toReversed() toSorted() toSpliced() toString() unshift() values() valueOf() with() JS Boolean