Before join you need map array

const array = [
    { a: '1', b: '2' },
    { a: '3', b: '4' },
];

const result = array.map(_ => _.a).join(', ');

console.log(result);

Answer from Nikita Madeev 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.
Discussions

Help with handling JSON array of objects (merge two arrays)
Split things up a bit and type your objects to make the logic easier to follow (this is off the top of my head, may not be 100% correct): interface Pipe { OD: number; SCH: {SCH40: number; SCH80: number}; Area?: number; // I think? } const getArea = (pipe: Pipe, sch: 'SCH40' | 'SCH80') => { return }; // Add an 'Area' property to each 'Pipe' object const pipesWithArea: Pipe[] = this.Pipesizes.map(x => ({...x, Area: getArea(x, this.selectedSCH)})); return pipesWithArea.find(x => x.Area >= this.OutsideNumber); More on reddit.com
🌐 r/typescript
14
14
April 23, 2020
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
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
jquery - Merge two json/javascript arrays in to one array - Stack Overflow
its dynamic. so I cant use index. I need a function which can merge n number of objects into final object. Murtaza Khursheed Hussain – Murtaza Khursheed Hussain · 2012-04-30 13:53:58 +00:00 Commented Apr 30, 2012 at 13:53 ... Those aren't JSON objects, just arrays. More on stackoverflow.com
🌐 stackoverflow.com
🌐
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.
🌐
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);

🌐
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.
🌐
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
🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › typescript › typescript_array_concat.htm
TypeScript - Array concat()
concat() method returns a new array comprised of this array joined with two or more arrays. valueN − Arrays and/or values to concatenate to the resulting array. Returns a new array.
🌐
CodeProject
codeproject.com › Questions › 5332641 › How-can-I-combine-some-arrays-to-create-a-unified
How can I combine some arrays to create a unified JSON in typescript / javascript - CodeProject
May 19, 2022 - Internet of Things · C / C++ / MFC> ATL / WTL / STL · Managed C++/CLI · C# Free Tools · Objective-C and Swift · Database · Hardware & Devices> System Admin · Hosting and Servers · Java · Linux Programming · Python · .NET (Core and Framework) Android ·
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

🌐
EyeHunts
tutorial.eyehunts.com › home › javascript join the array of objects | example code
JavaScript join the array of objects | Example code
February 28, 2024 - 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(","));
🌐
DevGenius
blog.devgenius.io › combining-json-objects-in-javascript-node-js-abe84f492d0
Combining JSON Objects in JavaScript/Node.js | by Popa Vlad | Dev Genius
March 6, 2025 - const jsonObj1 = { name: "John", age: 25 }; const jsonObj2 = { city: "New York", hobby: "Coding" }; const mergedJSON = { ...jsonObj1, ...jsonObj2 }; console.log(mergedJSON); This method has the same effect as using Object.assign(). ... Vlad is an IT enthusiast with experience in multiple areas of Technology.
🌐
Webdevtutor
webdevtutor.net › blog › typescript-join-array-of-objects
Mastering Array Join in TypeScript: Joining Arrays of Objects
Another approach is to use the ... 'Alice, Bob, Charlie' If you need to join multiple arrays of objects together, you can use methods like concat() or the spread operator to merge them....
🌐
Paige Niedringhaus
paigeniedringhaus.com › merge javascript objects in an array with different defined properties
Merge JavaScript Objects in an Array with Different Defined Properties | Paige Niedringhaus
June 6, 2022 - I needed to combine all the properties of objects that shared a common key along with their defined values into a new single object. And to make it just a smidge more difficult I needed it to work in TypeScript. 😅 · So today, I'll show you how to use JavaScript's reduce() function to merge two (or more) objects in an array with different defined properties.
Top answer
1 of 6
264

There are couple of different solutions to achieve this:

1 - Native javascript for-in loop:

const result = {};
let key;

for (key in obj1) {
  if(obj1.hasOwnProperty(key)){
    result[key] = obj1[key];
  }
}

for (key in obj2) {
  if(obj2.hasOwnProperty(key)){
    result[key] = obj2[key];
  }
}

2 - Object.keys():

const result = {};

Object.keys(obj1)
  .forEach(key => result[key] = obj1[key]);

Object.keys(obj2)
  .forEach(key => result[key] = obj2[key]);

3 - Object.assign():
(Browser compatibility: Chrome: 45, Firefox (Gecko): 34, Internet Explorer: No support, Edge: (Yes), Opera: 32, Safari: 9)

const result = Object.assign({}, obj1, obj2);

4 - Spread Operator:
Standardised from ECMAScript 2015 (6th Edition, ECMA-262):

Defined in several sections of the specification: Array Initializer, Argument Lists

Using this new syntax you could join/merge different objects into one object like this:

const result = {
  ...obj1,
  ...obj2,
};

5 - jQuery.extend(target, obj1, obj2):

Merge the contents of two or more objects together into the first object.

const target = {};

$.extend(target, obj1, obj2);

6 - jQuery.extend(true, target, obj1, obj2):

Run a deep merge of the contents of two or more objects together into the target. Passing false for the first argument is not supported.

const target = {};

$.extend(true, target, obj1, obj2);

7 - Lodash _.assignIn(object, [sources]): also named as _.extend:

const result = {};

_.assignIn(result, obj1, obj2);

8 - Lodash _.merge(object, [sources]):

const result = _.merge(obj1, obj2);

There are a couple of important differences between lodash's merge function and Object.assign:

1- Although they both receive any number of objects but lodash's merge apply a deep merge of those objects but Object.assign only merges the first level. For instance:

_.isEqual(_.merge({
  x: {
    y: { key1: 'value1' },
  },
}, {
  x: {
    y: { key2: 'value2' },
  },
}), {
  x: {
    y: {
      key1: 'value1',
      key2: 'value2',
    },
  },
}); // true

BUT:

const result = Object.assign({
  x: {
    y: { key1: 'value1' },
  },
}, {
  x: {
    y: { key2: 'value2' },
  },
});
_.isEqual(result, {
  x: {
    y: {
      key1: 'value1',
      key2: 'value2',
    },
  },
}); // false
// AND
_.isEqual(result, {
  x: {
    y: {
      key2: 'value2',
    },
  },
}); // true

2- Another difference has to do with how Object.assign and _.merge interpret the undefined value:

_.isEqual(_.merge({x: 1}, {x: undefined}), { x: 1 }) // false

BUT:

_.isEqual(Object.assign({x: 1}, {x: undefined}), { x: undefined })// true

Update 1:

When using for in loop in JavaScript, we should be aware of our environment specially the possible prototype changes in the JavaScript types. For instance some of the older JavaScript libraries add new stuff to Array.prototype or even Object.prototype. To safeguard your iterations over from the added stuff we could use object.hasOwnProperty(key) to mke sure the key is actually part of the object you are iterating over.


Update 2:

I updated my answer and added the solution number 4, which is a new JavaScript feature but not completely standardized yet. I am using it with Babeljs which is a compiler for writing next generation JavaScript.


Update 3:
I added the difference between Object.assign and _.merge.

2 of 6
15

WORKING FIDDLE

Simplest Way with Jquery -

var finalObj = $.extend(obj1, obj2);

Without Jquery -

var finalobj={};
for(var _obj in obj1) finalobj[_obj ]=obj1[_obj];
for(var _obj in obj2) finalobj[_obj ]=obj2[_obj];