1

I have this below json object:

{ 
 contacts:
 [ { id: 52,
     gsm: '919191919191',
     firstName: 'Ganesh' },
   { id: 51,
     gsm: '123456789',
     firstName: 'Mohamed' },
   { id: 53,
     gsm: '987654321',
     firstName: 'Mohamed' } ],

 groups:
 { contactsCount: 1,
   id: 40,
   groupname: 'Angular' }      
}

I want my final output like this:

[ { groupId:40 , contactId:52 }; { groupId:40 , contactId:51 } ; { groupId:40 , contactId:53 } ]

How to do using map or using any JS methods.

5
  • You are looking for a cartesian product of groups with contacts? Commented Jan 31, 2018 at 5:50
  • I want groupId from groups key and contactId from contacts key. Commented Jan 31, 2018 at 5:50
  • Are you sure you want them separated by ; sign, not :? { groupId:40 ; contactId:52 } or { groupId:40: contactId:52 } Commented Jan 31, 2018 at 5:51
  • See my updated question :) Commented Jan 31, 2018 at 5:52
  • Is this a Cartesian product or there are some id which map contact to group? Commented Jan 31, 2018 at 5:53

5 Answers 5

2

You can use array#map

var data = { contacts: [ { id: 52, gsm: '919191919191', firstName: 'Ganesh' }, { id: 51, gsm: '123456789', firstName: 'Mohamed' }, { id: 53, gsm: '987654321', firstName: 'Mohamed' } ], groups: { contactsCount: 1, id: 40, groupname: 'Angular' } },
    result = data.contacts.map(({id}) => ({groupId: data.groups.id, contactId: id}));
console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

2

One way to do this is:

var data = {
    contacts: [{
            id: 52,
            gsm: '919191919191',
            firstName: 'Ganesh'
        },
        {
            id: 51,
            gsm: '123456789',
            firstName: 'Mohamed'
        },
        {
            id: 53,
            gsm: '987654321',
            firstName: 'Mohamed'
        }
    ],

    groups: {
        contactsCount: 1,
        id: 40,
        groupname: 'Angular'
    }
};

function getMap(data) {
    return data.contacts.map((contact) => ({
        contactId: contact.id,
        groupId: data.groups.id
    }))
}
getMap(data); //[{"contactId":52,"groupId":40},{"contactId":51,"groupId":40},{"contactId":53,"groupId":40}]

Comments

2

You can use map

var output = contacts.map( s => Object.assign( {}, { contactId : s.id, groupId: groups.id } ) );

var contacts = [{
    id: 52,
    gsm: '919191919191',
    firstName: 'Ganesh'
  },
  {
    id: 51,
    gsm: '123456789',
    firstName: 'Mohamed'
  },
  {
    id: 53,
    gsm: '987654321',
    firstName: 'Mohamed'
  }
];

var groups = {
  contactsCount: 1,
  id: 40,
  groupname: 'Angular'
};

console.log(contacts.map( s => Object.assign( {}, { contactId : s.id, groupId: groups.id } ) ));

2 Comments

see my json object in question , contacts and groups inside one object.
You can apply the same logic there. Just that how you access the object will change. contacts.map will become obj.contacts.map and groups.id will become obj.groups.id.
1

Late to the party but here you go:

var data = { 
 contacts:
 [ { id: 52,
     gsm: '919191919191',
     firstName: 'Ganesh' },
   { id: 51,
     gsm: '123456789',
     firstName: 'Mohamed' },
   { id: 53,
     gsm: '987654321',
     firstName: 'Mohamed' } ],

 groups:
 { contactsCount: 1,
   id: 40,
   groupname: 'Angular' }      
};

var newArray = [];

for ( var i=0;i < data.contacts.length;i++ ) {    
  newArray.push({"groupId":data.groups.id});
  newArray[i]["contactId"] = data.contacts[i].id;
}

console.log(JSON.stringify(newArray));

Comments

0

Here is my response

let obj = {
  contacts:
    [
      {
        id: 52,
        gsm: '919191919191',
        firstName: 'Ganesh'
      },
      {
        id: 51,
        gsm: '123456789',
        firstName: 'Mohamed'
      },
      {
        id: 53,
        gsm: '987654321',
        firstName: 'Mohamed'
      }
    ],
  groups: {
    contactsCount: 1,
    id: 40,
    groupname: 'Angular'
  }
}

let result = obj.contacts.map(contact => {
  return {groupId: obj.groups.id, contactId: contact.id}
})

console.log(result)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.