I want to take an array that looks like the following:
我想采用如下所示的数组:
var contacts = [
{account: 'Acme', firstName: 'John', lastName: 'Snow'},
{account: 'Metal Industries', firstName: 'Ted', lastName: 'Smith'}
];
Note: contacts can have the same account name.
注意:联系人可以具有相同的帐户名称。
and create a function to convert the data to something that instead is an object with the following structure that's like a map where the key is account name and value is an array of alphabetized full names as below:
并创建一个函数将数据转换为具有以下结构的对象,该结构类似于一个映射,其中键是帐户名,值是一个按字母顺序排列的全名的数组,如下所示:
var acctContactObject = {
'Acme': ['John Snow','Kyle Johnson','Sara Butler'],
'HiTech Corp': ['Arnold Williams','Jason Fernandez','Sam Johnson']
};
I'm not certain that I'm taking the correct approach and wanted to seek some sage advice before proceeding. Here's what I've written so far and "psuedocode" for where I'm heading.
我不确定我采取了正确的方法,并希望在继续之前寻求一些圣人的建议。这是我到目前为止所写的内容,以及“psuedocode”代码。
function convertAccountArrayToObject(contacts){
this.account = contacts.account;
this.firstName = contacts.firstName;
this.lastName = contacts.lastName;
this.sort(function(a, b) {
return a.account.localeCompare(b.account);
});
var aco = new acctContactObject();
var name;
var nameArray = [];
for(var member of this){
//this is where I get stuck
//I could create a new array to hold the account with full name
//but somehow need to add them to an array that I can sort on
//
//assuming I used an intermediate array...
//create new acctContactObject
//For acct in intermediate array
//add name to another single array and sort alphabetically
//put acct name and sorted list of names into new object
//after end of loop, return object
I've been searching to see if there's a way to do a secondary sort on a multidimensional array, but didn't find what I was looking for. I've also run across mention of "merging" properties, but am not certain if that would allow me to concatenate the values for the first and last names properly.
我一直在寻找是否有办法在多维数组上进行二次排序,但没有找到我想要的东西。我也提到了“合并”属性,但我不确定是否允许我正确地连接名字和姓氏的值。
I don't do a lot of JS, at least not of this kind, so would appreciate knowing if I'm on the right track. Any suggestions or guidance towards a cleaner way to approach this would be appreciated.
我不做很多JS,至少不是这种,所以如果我走在正确的轨道上会很感激。任何建议或指导,以更清洁的方式来处理这一点将不胜感激。
1 个解决方案
#1
0
The simplest way to do it will be as follows.
最简单的方法如下。
Note:I have modified contacts to include the case of same account names.
注意:我修改了联系人以包含相同帐户名称的大小写。
var contacts = [
{account: 'Acme', firstName: 'John', lastName: 'Snow'},
{account: 'Metal Industries', firstName: 'Ted', lastName: 'Smith'},
{account: 'Metal Industries', firstName: 'Bolt', lastName: 'Coder'}
];
function convertAccountArrayToObject(contacts){
var returnObj={},key;
contacts.forEach(function(v,i){
key=v.account;
if(!returnObj[key]){
returnObj[key]=[];
}
returnObj[key].push(v.firstName+" "+v.lastName);
});
return returnObj;
}
acctContactObject=convertAccountArrayToObject(contacts);
Upvote if you find this helpful.
如果您觉得有用,请进行Upvote。
#1
0
The simplest way to do it will be as follows.
最简单的方法如下。
Note:I have modified contacts to include the case of same account names.
注意:我修改了联系人以包含相同帐户名称的大小写。
var contacts = [
{account: 'Acme', firstName: 'John', lastName: 'Snow'},
{account: 'Metal Industries', firstName: 'Ted', lastName: 'Smith'},
{account: 'Metal Industries', firstName: 'Bolt', lastName: 'Coder'}
];
function convertAccountArrayToObject(contacts){
var returnObj={},key;
contacts.forEach(function(v,i){
key=v.account;
if(!returnObj[key]){
returnObj[key]=[];
}
returnObj[key].push(v.firstName+" "+v.lastName);
});
return returnObj;
}
acctContactObject=convertAccountArrayToObject(contacts);
Upvote if you find this helpful.
如果您觉得有用,请进行Upvote。