-5

---- Object 1 ----

var sib1 =  {
        fname: 'sangram',
        lname: 'jadha',
        image: 'male.jpg',
        id: '1',
}

---- Object 2 ----

var sib2 =  {
            fname: 'sam',
            lname: 'jad',
            image: 'male.jpg',
            id: '2',
    }

----- Expected Output --------

var sib3 =  {
           {
              fname: 'sangram',
              lname: 'jadha',
              image: 'male.jpg',
              id: '1',
            },
            {
                fname: 'sam',
                lname: 'jad',
                image: 'male.jpg',
                id: '2',
            }
           }
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
  • Have a look at this question: [How to join two json object in javascript, without using JQUERY](https://stackoverflow.com/questions/21450060/how-to-join-two-json-object-in-javascript-without-using-jquery) There is also a jQuery solution. – Colin Jul 26 '17 at 05:55
  • 1
    I am sure you don't want that output. Do you mean it to be an array instead? – Frederik.L Jul 26 '17 at 05:55
  • 1
    Acc to me, your expected output isn't possible. You can have an array of objects, but var sib3 = {..},{..} Seems invalid to me – Abhishek Ranjan Jul 26 '17 at 05:56
  • 1
    I think everyone who ever has answered this kind of question, should be downvoted along with the asker. Answering to such a question where there are not a single effort, not even proper output should be discouraged – undefined Jul 26 '17 at 07:07
  • Hi @Sangram , please first be sure what output you are expecting.. your output even after edit is not possible – undefined Jul 26 '17 at 07:09
  • 1
    It would be infamous but great, to prevent new users to answer a question if it has a negative score. As it is, new users are digging reputation and will try to answer any shitty question. That gives them the illusion of being useful and at the end of the day their reputation won't go up. – Frederik.L Jul 26 '17 at 07:14
  • Possible duplicate of [How to merge two object values by keys](https://stackoverflow.com/questions/18498801/how-to-merge-two-object-values-by-keys) – Makyen Aug 05 '17 at 23:47

6 Answers6

0

var sib1 =  {
        fname: 'sangram',
        lname: 'jadha',
        image: 'male.jpg',
        id: '1',
};

var sib2 =  {
            fname: 'sam',
            lname: 'jad',
            image: 'male.jpg',
            id: '2',
    };
    
    var sib3 = [];
    sib3.push(sib1);
    sib3.push(sib2);
    console.log(JSON.stringify(sib1)+","+JSON.stringify(sib2));
Arunkumar G
  • 126
  • 6
0

You can simply push the objects one after the other into sib3

sib3  =  new Array();

sib3.push(sib1);
sib3.push(sib2);

This has to work if you are looking for an array of objects derived from different individual objects.

Strikers
  • 4,698
  • 2
  • 28
  • 58
0

You can use array#push to get an array.

var sib1 =  {
        fname: 'sangram',
        lname: 'jadha',
        image: 'male.jpg',
        id: '1',
}

var sib2 =  {
        fname: 'sam',
        lname: 'jad',
        image: 'male.jpg',
        id: '2',
}

var sib3 = [];
sib3.push(sib1);
sib3.push(sib2);

console.log(sib3);

In case you want object.

var sib1 =  {
        fname: 'sangram',
        lname: 'jadha',
        image: 'male.jpg',
        id: '1',
}

var sib2 =  {
        fname: 'sam',
        lname: 'jad',
        image: 'male.jpg',
        id: '2',
}

var result = {
  sib1,
  sib2
};
console.log(result);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
0

Create a new object where sib1 and sib2 are properties of that object.

var sib1 =  {
    fname: 'sangram',
    lname: 'jadha',
    image: 'male.jpg',
    id: '1',
};

var sib2 =  {
    fname: 'sam',
    lname: 'jad',
    image: 'male.jpg',
    id: '2',
};

var sib3 = {sib1: sib1, sib2: sib2}

But again the resulted object is not exactly what you are looking for and making that object is not possible at all. Because it is invalid.

Kavindra
  • 1,697
  • 2
  • 14
  • 19
0

INVALID OBJECT var sib3 = {name: 'sangram',lname: 'jadha',image: 'male.jpg',id: '1',},{ fname: 'sam', lname: 'jad',image: 'male.jpg',id: '2',}

This abject sib3 is not in valid format, you can not use object like this.

Valid format of writing such object is as follows:

 var sib3 =  [{name: 'sangram',lname: 'jadha',image: 'male.jpg',id: '1',},{  fname: 'sam', lname: 'jad',image: 'male.jpg',id: '2',}]

To acchive the same(a valid format array) use following code:

 var sib1 = { fname: 'sangram', lname: 'jadha', image: 'male.jpg', id: '1' }
  var sib2 = { fname: 'sam', lname: 'jad', image: 'male.jpg', id: '2' }
 var sib3 =[]; 
 sib3.push(sib1);
 sib3.push(sib2);
console.log(sib3)

If you still want to object look like as shown in question, it would be type of string not object. See the following lines of code:

var sib1 = { fname: 'sangram', lname: 'jadha', image: 'male.jpg', id: '1' }
      var sib2 = { fname: 'sam', lname: 'jad', image: 'male.jpg', id: '2' }
     var sib3 =[]; 
     sib3.push(sib1);
     sib3.push(sib2);
   var newObjAsString = JSON.stringify(sib3).substring(1,JSON.stringify(sib3).length - 1);
   
console.log(newObjAsString )

Using above line of code it will look like as expected but this is not an object type.

Arun Saini
  • 6,714
  • 1
  • 19
  • 22
-1

I think you are looking for the Object.assign() method that will return a new object with the properties overridden of the objects you pass in.

var sib3 = Object.assign({},sib1, sib2);

An other possibility is that you want sib3 to be an array, then you could just do

sib3 = [sib1, sib2]

    var sib1 =  {
        fname: 'sangram',
        lname: 'jadha',
        image: 'male.jpg',
        id: '1',
    }
    var sib2 =  {
            fname: 'sam',
            lname: 'jad',
            image: 'male.jpg',
            id: '2',
    }
    var sib3 = Object.assign({},sib1, sib2);
    console.log(sib3)
Alejandro Vales
  • 2,816
  • 22
  • 41
  • I tried this but it give me output like this. [{fname:"S", lname:"1", image:"http://qabilati.wiseprm.com/uploads/profileimage/male.jpg", id:"8"}, {fname:"S", lname:"2", image:"http://qabilati.wiseprm.com/uploads/profileimage/female.jpg", id:"9"}] I don't want sqaure([ ]) brackets in output – Sangram Jadhav Jul 26 '17 at 05:59
  • As said in the comment of your answer, there is no way an object can look like your `sib3` there is no sense for that to exist, please, take a look into javascript types thank you :) – Alejandro Vales Jul 26 '17 at 06:01
  • I am using d3.js. Here required object start with "{" and end with "}" – Sangram Jadhav Jul 26 '17 at 06:03
  • Then what you are looking is to merge the 2 objects together and have an other one, (take a look into the code snippet) or you need to use in the d3 a method that allows the creation of an array of objects (that will need of those "square brackets") – Alejandro Vales Jul 26 '17 at 06:05
  • 1
    @SangramJadhav Why are jquery and angularjs in the tags if the question is about d3.js? Please update your question accordingly so people stop rushing misguided answers. – Frederik.L Jul 26 '17 at 06:47
  • @Frederik.L the worse thing is that when I told him to take a look into javascript types he got offended and downrated my question... I really sometimes don't understand this people... – Alejandro Vales Jul 26 '17 at 07:00