Join array of object to new array object

javascript merge array of objects
javascript merge multiple array of objects
javascript add array to array
merge two arrays with same keys javascript
array join javascript
javascript array to string without commas
javascript map
object to array javascript

I have data array object like this:

const data = [
  {Name: "dian", Job: "programmer"},
  {Name: "dian", Job: "gamer"},
  {Name: "candra", Job: "programmer"},

My goal is to create new data where a have same value join b.

Example output:

const new_data = [
  {Name: "dian", Jobs: [{Job: "programmer"}, {Job: "gamer"}]},
  {Name: "candra", Jobs: [{Job: "programmer"}]},

use reduce.

const data = [
  { Name: "dian", Job: "programer" },
  { Name: "dian", Job: "gamers" },
  { Name: "candra", Job: "programer" }

const output = Object.values(data.reduce((a, { Name, Job }, i) => {
  if (!a[Name]) {
    a[Name] = { Name, Jobs: [] };
  a[Name].Jobs.push({ Job });
  return a;

}, {}));


Array.prototype.join(), Here are 2 ways to combine your arrays and return a NEW array. I like using the Spread operator. But if you need older browser support, you should use Concat. The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

I think you can use Array.prototype.reduce() to achive your goal. From the documentation:

The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.

One possible solution:

const data = [
  {Name:'dian', Job:'programer'}, 
  {Name:'dian', Job:'gamers'}, 
  {Name:'candra', Job:'programer'}

const result = data.reduce((a, current) => {
  const found = a.find(f => f.Name === current.Name);
  if (found) {
     found.Jobs.push({Job: current.Job});
  } else {
      Name: current.Name,
      Jobs: [
        { Job: current.Job }
  return a;
}, []);


Array.prototype.concat(), Covers join method plus type conversion of primitive values and objects to The join method converts the elements of an array to strings, concatenates JavaScript is a loosely-typed language and will convert from one type to another when� Arrays of objects don't stay the same all the time. We almost always need to manipulate them. So let's take a look at how we can add objects to an already existing array. Add a new object at the start - Array.unshift. To add an object at the first position, use Array.unshift.

Here's one approach:

const combineJobs = (data) =>
  Object .values (data .reduce (
    (a, {Name, Job}, _, __, curr = a [Name] || {Name, jobs: []}) => 
      ({... a, [Name]: ({... curr, jobs: [... curr .jobs, {Job}]})}),

const data = [{Name: "dian", Job: "programmer"}, {Name: "dian", Job: "gamer"}, {Name: "candra", Job: "programmer"}]

console .log (combineJobs (data))

2 Ways to Merge Arrays in JavaScript, is a very powerful method in JavaScript. attached to these objects. .call() allows us to utilize the context of one object on another. If the only argument passed to the Array constructor is an integer between 0 and 2 32-1 (inclusive), this returns a new JavaScript array with its length property set to that number (Note: this implies an array of arrayLength empty slots, not slots with actual undefined values).

function modifyArray(data) {
    function getNewObject(data) {
        const newObject = {
            Name: data.Name
        Object.keys(data).filter(key => key !== 'Name').forEach(key => {
             newObject[key+'s'] = [];
                 [key]: data[key]
        return newObject;
    function appendData(obj, data) {
        Object.keys(data).filter(key => key !== 'Name').forEach(key => {
                 [key]: data[key]
    const reqArray = [];
    data.forEach(d => {
        const objToModify = reqArray.find(a => a.Name === d.Name);
        if (!objToModify) {
        } else {
            appendData(objToModify, d);
    return reqArray;

let data =[ 
    {Name:'dian', Job:'programer' }, 
    {Name:'dian', Job:'gamers' }, 
    {Name:'candra', Job:'programer' }


Converting JavaScript Arrays to Strings, The first array-like object to merge, the elements of second added. second This shortcut creates a new, empty array and merges the contents of oldArray into it,� Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

4 Uses of JavaScript's You Should Know ―, Join two arrays: This method does not change the existing arrays, but returns a new array, Return Value: An Array object, representing the joined array. don't worry about inner objects but just wanted to a make structure like this: The new Object .fromEntries, from makes it even easier to transform values from

jQuery.merge(), The concatenated array is returned as a new object. If you want to merge two arrays without creating a new object, you can use the following technique:. Convert Object to Array Example. This section will see how we can convert Object to Array in Angular and Typescript with examples. Typescript Code. Let us assume that you have an object declared multiple properties. First Get the named keys using object.keys() method. This method retrieves keys from the given object and returns an array of keys.

JavaScript Array concat() Method, The arrays are objects, so we can try to use delete : The method arr.concat creates a new array that includes values from other arrays and� Given two arrays of the same type, they need to be merged into a new object array. The task is to merge the two arrays of the same type into an object array such that the array elements maintain their original order in the newly merged array and the elements of the first array precedes the elements of the second array in the merged object array.

  • Welcome to SO, what did you try? You're expected to at least try and we can help if you have issues
  • Code-only answers are rarely appropriate here. Please add at least a little explanation.