使用map-Javascript转换数组

时间:2018-01-31 05:49:00

标签: javascript arrays json object

我在json对象下面有这个:

{ 
 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' }      
}

我希望我的最终输出如下:

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

如何使用map或使用任何JS方法。

5 个答案:

答案 0 :(得分:2)

一种方法是:

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}]

答案 1 :(得分:2)

您可以使用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 :(得分:2)

您可以使用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);

答案 3 :(得分:1)

参加晚会,但是你走了:



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));
&#13;
&#13;
&#13;

答案 4 :(得分:0)

以下是我的回复

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)