Map 3 arrays of objects by key in JavaScriptAnalog to SQL 'JOIN' for Javascript objects?How do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How do I include a JavaScript file in another JavaScript file?Checking if a key exists in a JavaScript object?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
What is the most common color to indicate the input-field is disabled?
How can I prove that a state of equilibrium is unstable?
Does Dispel Magic work on Tiny Hut?
Is "/bin/[.exe" a legitimate file? [Cygwin, Windows 10]
What's the meaning of "Sollensaussagen"?
Is this draw by repetition?
Can someone clarify Hamming's notion of important problems in relation to modern academia?
How could indestructible materials be used in power generation?
Am I breaking OOP practice with this architecture?
How seriously should I take size and weight limits of hand luggage?
What Exploit Are These User Agents Trying to Use?
Obtaining database information and values in extended properties
Why was Sir Cadogan fired?
Was the Stack Exchange "Happy April Fools" page fitting with the '90's code?
How to stretch the corners of this image so that it looks like a perfect rectangle?
What would the climate of a planetwide city be like?
What historical events would have to change in order to make 19th century "steampunk" technology possible?
How dangerous is XSS
Does the Idaho Potato Commission associate potato skins with healthy eating?
Can a virus destroy the BIOS of a modern computer?
What reasons are there for a Capitalist to oppose a 100% inheritance tax?
Why didn't Boeing produce its own regional jet?
GFCI outlets - can they be repaired? Are they really needed at the end of a circuit?
Different meanings of こわい
Map 3 arrays of objects by key in JavaScript
Analog to SQL 'JOIN' for Javascript objects?How do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How do I include a JavaScript file in another JavaScript file?Checking if a key exists in a JavaScript object?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
I have 3 sets of objects, 1 Set can be map to a corresponding set by a unique key. That same set can be mapped to the 3rd set by a different key. I need to be able to map all of these into a new combined set. These sets all have different properties.
Unique Count [users][sector]
Many Count [invoices]
Each unique [user] belongs to a specific [sector] found by (comitID), that same [user] can have many [invoices] though. A one to many field if your familiar with Relational Databases
const users= [ // Unique Entries
name:'user1', comitId: 'aa1',
name:'user2', comitId: 'aa2'
]
const sector= [ // Unique Entries
comitID: 'aa1', department: 'finance',
comitID: 'aa2', department: 'marketing',
comitID: 'aa3', department: 'engineering'
]
const invoices= [ // Multiple Entries
name: 'user1' : statementDate: '2/1/2019',
name: 'user1' : statementDate: '2/14/2019',
name: 'user2' : statementDate: '2/1/2019'
]
The new set should look like this. Cannot contain a list for the statement dates, they each need to be a new object.
const results = [
name: 'user1', comitId: 'aa1', department: 'finance', statementDate: '2/1/2019',
name: 'user1', comitId: 'aa1', department: 'finance', statementDate: '2/14/2019',
name: 'user2', comitId: 'aa2', department: 'marketing', statementDate: '2/1/2019'
]
I have been trying this in Excel with vlookups and formulas. These files tend to be 10k for the unique counts and up 40k for the invoices.
javascript mysql node.js database
add a comment |
I have 3 sets of objects, 1 Set can be map to a corresponding set by a unique key. That same set can be mapped to the 3rd set by a different key. I need to be able to map all of these into a new combined set. These sets all have different properties.
Unique Count [users][sector]
Many Count [invoices]
Each unique [user] belongs to a specific [sector] found by (comitID), that same [user] can have many [invoices] though. A one to many field if your familiar with Relational Databases
const users= [ // Unique Entries
name:'user1', comitId: 'aa1',
name:'user2', comitId: 'aa2'
]
const sector= [ // Unique Entries
comitID: 'aa1', department: 'finance',
comitID: 'aa2', department: 'marketing',
comitID: 'aa3', department: 'engineering'
]
const invoices= [ // Multiple Entries
name: 'user1' : statementDate: '2/1/2019',
name: 'user1' : statementDate: '2/14/2019',
name: 'user2' : statementDate: '2/1/2019'
]
The new set should look like this. Cannot contain a list for the statement dates, they each need to be a new object.
const results = [
name: 'user1', comitId: 'aa1', department: 'finance', statementDate: '2/1/2019',
name: 'user1', comitId: 'aa1', department: 'finance', statementDate: '2/14/2019',
name: 'user2', comitId: 'aa2', department: 'marketing', statementDate: '2/1/2019'
]
I have been trying this in Excel with vlookups and formulas. These files tend to be 10k for the unique counts and up 40k for the invoices.
javascript mysql node.js database
do you have anything you have tried? This really does look like something you would solve in excel or sql.
– SpeedOfRound
Mar 8 at 20:22
Here's a similar question that might help: Analog to SQL 'JOIN' for Javascript objects?
– nireno
Mar 8 at 22:21
add a comment |
I have 3 sets of objects, 1 Set can be map to a corresponding set by a unique key. That same set can be mapped to the 3rd set by a different key. I need to be able to map all of these into a new combined set. These sets all have different properties.
Unique Count [users][sector]
Many Count [invoices]
Each unique [user] belongs to a specific [sector] found by (comitID), that same [user] can have many [invoices] though. A one to many field if your familiar with Relational Databases
const users= [ // Unique Entries
name:'user1', comitId: 'aa1',
name:'user2', comitId: 'aa2'
]
const sector= [ // Unique Entries
comitID: 'aa1', department: 'finance',
comitID: 'aa2', department: 'marketing',
comitID: 'aa3', department: 'engineering'
]
const invoices= [ // Multiple Entries
name: 'user1' : statementDate: '2/1/2019',
name: 'user1' : statementDate: '2/14/2019',
name: 'user2' : statementDate: '2/1/2019'
]
The new set should look like this. Cannot contain a list for the statement dates, they each need to be a new object.
const results = [
name: 'user1', comitId: 'aa1', department: 'finance', statementDate: '2/1/2019',
name: 'user1', comitId: 'aa1', department: 'finance', statementDate: '2/14/2019',
name: 'user2', comitId: 'aa2', department: 'marketing', statementDate: '2/1/2019'
]
I have been trying this in Excel with vlookups and formulas. These files tend to be 10k for the unique counts and up 40k for the invoices.
javascript mysql node.js database
I have 3 sets of objects, 1 Set can be map to a corresponding set by a unique key. That same set can be mapped to the 3rd set by a different key. I need to be able to map all of these into a new combined set. These sets all have different properties.
Unique Count [users][sector]
Many Count [invoices]
Each unique [user] belongs to a specific [sector] found by (comitID), that same [user] can have many [invoices] though. A one to many field if your familiar with Relational Databases
const users= [ // Unique Entries
name:'user1', comitId: 'aa1',
name:'user2', comitId: 'aa2'
]
const sector= [ // Unique Entries
comitID: 'aa1', department: 'finance',
comitID: 'aa2', department: 'marketing',
comitID: 'aa3', department: 'engineering'
]
const invoices= [ // Multiple Entries
name: 'user1' : statementDate: '2/1/2019',
name: 'user1' : statementDate: '2/14/2019',
name: 'user2' : statementDate: '2/1/2019'
]
The new set should look like this. Cannot contain a list for the statement dates, they each need to be a new object.
const results = [
name: 'user1', comitId: 'aa1', department: 'finance', statementDate: '2/1/2019',
name: 'user1', comitId: 'aa1', department: 'finance', statementDate: '2/14/2019',
name: 'user2', comitId: 'aa2', department: 'marketing', statementDate: '2/1/2019'
]
I have been trying this in Excel with vlookups and formulas. These files tend to be 10k for the unique counts and up 40k for the invoices.
javascript mysql node.js database
javascript mysql node.js database
edited Mar 10 at 16:23
halfer
14.7k759116
14.7k759116
asked Mar 8 at 20:14
Christopher BradleyChristopher Bradley
105
105
do you have anything you have tried? This really does look like something you would solve in excel or sql.
– SpeedOfRound
Mar 8 at 20:22
Here's a similar question that might help: Analog to SQL 'JOIN' for Javascript objects?
– nireno
Mar 8 at 22:21
add a comment |
do you have anything you have tried? This really does look like something you would solve in excel or sql.
– SpeedOfRound
Mar 8 at 20:22
Here's a similar question that might help: Analog to SQL 'JOIN' for Javascript objects?
– nireno
Mar 8 at 22:21
do you have anything you have tried? This really does look like something you would solve in excel or sql.
– SpeedOfRound
Mar 8 at 20:22
do you have anything you have tried? This really does look like something you would solve in excel or sql.
– SpeedOfRound
Mar 8 at 20:22
Here's a similar question that might help: Analog to SQL 'JOIN' for Javascript objects?
– nireno
Mar 8 at 22:21
Here's a similar question that might help: Analog to SQL 'JOIN' for Javascript objects?
– nireno
Mar 8 at 22:21
add a comment |
5 Answers
5
active
oldest
votes
You can use Array.map() over invoices and Array.find() to get the corresponding entries in users and then sectors:
const users = [ // Unique Entries
name:'user1', comitId: 'aa1',
name:'user2', comitId: 'aa2'
];
const sectors = [ // Unique Entries
comitID: 'aa1', department: 'finance',
comitID: 'aa2', department: 'marketing',
comitID: 'aa3', department: 'engineering'
];
const invoices = [ // Multiple Entries
name: 'user1', statementDate: '2/1/2019',
name: 'user1', statementDate: '2/14/2019',
name: 'user2', statementDate: '2/1/2019'
];
const result = invoices.map(invoice => );
console.log(result);
This works, thank you. Is this the most optimized way, the invoices can be really huge 40k objects.
– Christopher Bradley
Mar 9 at 13:58
add a comment |
I would suggest that you just iterate over the invoices and enrich the entries with the ones from the sets with unique entries.
Something like this (does not tested the code, but I hope you can understand the idea)
const data = invoices
.map(entry => ...entry, ...
users.find(user => user.name === entry.name)
.map(user => ...user,
sector.find(sec=> sec.comitID === user.commitID)
)
)
You could improve speed when you first create a map out of the sets and then just lookup the join attributes instead of searching for them
const userMap = users.reduce((map, user) => ...map, ...user.name: user, )
const sectorMap = sector.reduce((map, sec) => ...map, ...sector.comitID: sec), )
const data = invoices.map(invoice => ...invoice, ...userMap[invoice.name], ...sector[userMap[invoice.name].comitID])
add a comment |
Here's a basic script that could work.
const users = [ // Unique Entries
name:'user1', comitId: 'aa1',
name:'user2', comitId: 'aa2'
]
const sectors = [ // Unique Entries
comitID: 'aa1', department: 'finance',
comitID: 'aa2', department: 'marketing',
comitID: 'aa3', department: 'engineering'
]
const invoices = [ // Multiple Entries
name: 'user1', statementDate: '2/1/2019',
name: 'user1', statementDate: '2/14/2019',
name: 'user2', statementDate: '2/1/2019'
]
sectors.forEach(sector =>
const user = users.find(user => sector.comitID === user.comitId);
if (user)
user.department = sector.department;
);
const results = invoices.map(invoice =>
const user = users.find(user => invoice.name === user.name);
return Object.assign(, user, statementDate: invoice.statementDate );
);
console.log(results);
add a comment |
You can use map & filter
const users = [ // Unique Entries
name: 'user1',
comitId: 'aa1'
,
name: 'user2',
comitId: 'aa2'
]
const sector = [ // Unique Entries
comitID: 'aa1',
department: 'finance'
,
comitID: 'aa2',
department: 'marketing'
,
comitID: 'aa3',
department: 'engineering'
]
const invoices = [ // Multiple Entries
name: 'user1',
statementDate: '2/1/2019'
,
name: 'user1',
statementDate: '2/14/2019'
,
name: 'user2',
statementDate: '2/1/2019'
]
let newArray = invoices.map(function(item)
// this value will be use to find match between users & sectors
let cId = users.filter(user => user.name === item.name)[0].comitId;
return
name: item.name,
statementDate: item.statementDate,
comitId: cId,
department: sector.filter(sector => sector.comitID === cId)[0].department
);
console.log(newArray)add a comment |
You could move user and sector items into a map and take this object, if necessary.
const
users = [ name: 'user1', comitId: 'aa1' , name: 'user2', comitId: 'aa2' ],
sector = [ comitID: 'aa1', department: 'finance' , comitID: 'aa2', department: 'marketing' , comitID: 'aa3', department: 'engineering' ],
invoices = [ name: 'user1', statementDate: '2/1/2019', name: 'user1', statementDate: '2/14/2019' , name: 'user2', statementDate: '2/1/2019' ],
setMap = k => (m, o) => m.set(o[k], o),
userMap = users.reduce(setMap('name'), new Map),
sectorMap = sector.reduce(setMap('comitID'), new Map),
result = invoices.map(( name, statementDate ) =>
var comitId = userMap.get(name),
department = sectorMap.get(comitId);
return name, comitId, department, statementDate ;
);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55070403%2fmap-3-arrays-of-objects-by-key-in-javascript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use Array.map() over invoices and Array.find() to get the corresponding entries in users and then sectors:
const users = [ // Unique Entries
name:'user1', comitId: 'aa1',
name:'user2', comitId: 'aa2'
];
const sectors = [ // Unique Entries
comitID: 'aa1', department: 'finance',
comitID: 'aa2', department: 'marketing',
comitID: 'aa3', department: 'engineering'
];
const invoices = [ // Multiple Entries
name: 'user1', statementDate: '2/1/2019',
name: 'user1', statementDate: '2/14/2019',
name: 'user2', statementDate: '2/1/2019'
];
const result = invoices.map(invoice => );
console.log(result);
This works, thank you. Is this the most optimized way, the invoices can be really huge 40k objects.
– Christopher Bradley
Mar 9 at 13:58
add a comment |
You can use Array.map() over invoices and Array.find() to get the corresponding entries in users and then sectors:
const users = [ // Unique Entries
name:'user1', comitId: 'aa1',
name:'user2', comitId: 'aa2'
];
const sectors = [ // Unique Entries
comitID: 'aa1', department: 'finance',
comitID: 'aa2', department: 'marketing',
comitID: 'aa3', department: 'engineering'
];
const invoices = [ // Multiple Entries
name: 'user1', statementDate: '2/1/2019',
name: 'user1', statementDate: '2/14/2019',
name: 'user2', statementDate: '2/1/2019'
];
const result = invoices.map(invoice => );
console.log(result);
This works, thank you. Is this the most optimized way, the invoices can be really huge 40k objects.
– Christopher Bradley
Mar 9 at 13:58
add a comment |
You can use Array.map() over invoices and Array.find() to get the corresponding entries in users and then sectors:
const users = [ // Unique Entries
name:'user1', comitId: 'aa1',
name:'user2', comitId: 'aa2'
];
const sectors = [ // Unique Entries
comitID: 'aa1', department: 'finance',
comitID: 'aa2', department: 'marketing',
comitID: 'aa3', department: 'engineering'
];
const invoices = [ // Multiple Entries
name: 'user1', statementDate: '2/1/2019',
name: 'user1', statementDate: '2/14/2019',
name: 'user2', statementDate: '2/1/2019'
];
const result = invoices.map(invoice => );
console.log(result);You can use Array.map() over invoices and Array.find() to get the corresponding entries in users and then sectors:
const users = [ // Unique Entries
name:'user1', comitId: 'aa1',
name:'user2', comitId: 'aa2'
];
const sectors = [ // Unique Entries
comitID: 'aa1', department: 'finance',
comitID: 'aa2', department: 'marketing',
comitID: 'aa3', department: 'engineering'
];
const invoices = [ // Multiple Entries
name: 'user1', statementDate: '2/1/2019',
name: 'user1', statementDate: '2/14/2019',
name: 'user2', statementDate: '2/1/2019'
];
const result = invoices.map(invoice => );
console.log(result);const users = [ // Unique Entries
name:'user1', comitId: 'aa1',
name:'user2', comitId: 'aa2'
];
const sectors = [ // Unique Entries
comitID: 'aa1', department: 'finance',
comitID: 'aa2', department: 'marketing',
comitID: 'aa3', department: 'engineering'
];
const invoices = [ // Multiple Entries
name: 'user1', statementDate: '2/1/2019',
name: 'user1', statementDate: '2/14/2019',
name: 'user2', statementDate: '2/1/2019'
];
const result = invoices.map(invoice => );
console.log(result);const users = [ // Unique Entries
name:'user1', comitId: 'aa1',
name:'user2', comitId: 'aa2'
];
const sectors = [ // Unique Entries
comitID: 'aa1', department: 'finance',
comitID: 'aa2', department: 'marketing',
comitID: 'aa3', department: 'engineering'
];
const invoices = [ // Multiple Entries
name: 'user1', statementDate: '2/1/2019',
name: 'user1', statementDate: '2/14/2019',
name: 'user2', statementDate: '2/1/2019'
];
const result = invoices.map(invoice => );
console.log(result);answered Mar 8 at 20:29
jo_vajo_va
7,7582930
7,7582930
This works, thank you. Is this the most optimized way, the invoices can be really huge 40k objects.
– Christopher Bradley
Mar 9 at 13:58
add a comment |
This works, thank you. Is this the most optimized way, the invoices can be really huge 40k objects.
– Christopher Bradley
Mar 9 at 13:58
This works, thank you. Is this the most optimized way, the invoices can be really huge 40k objects.
– Christopher Bradley
Mar 9 at 13:58
This works, thank you. Is this the most optimized way, the invoices can be really huge 40k objects.
– Christopher Bradley
Mar 9 at 13:58
add a comment |
I would suggest that you just iterate over the invoices and enrich the entries with the ones from the sets with unique entries.
Something like this (does not tested the code, but I hope you can understand the idea)
const data = invoices
.map(entry => ...entry, ...
users.find(user => user.name === entry.name)
.map(user => ...user,
sector.find(sec=> sec.comitID === user.commitID)
)
)
You could improve speed when you first create a map out of the sets and then just lookup the join attributes instead of searching for them
const userMap = users.reduce((map, user) => ...map, ...user.name: user, )
const sectorMap = sector.reduce((map, sec) => ...map, ...sector.comitID: sec), )
const data = invoices.map(invoice => ...invoice, ...userMap[invoice.name], ...sector[userMap[invoice.name].comitID])
add a comment |
I would suggest that you just iterate over the invoices and enrich the entries with the ones from the sets with unique entries.
Something like this (does not tested the code, but I hope you can understand the idea)
const data = invoices
.map(entry => ...entry, ...
users.find(user => user.name === entry.name)
.map(user => ...user,
sector.find(sec=> sec.comitID === user.commitID)
)
)
You could improve speed when you first create a map out of the sets and then just lookup the join attributes instead of searching for them
const userMap = users.reduce((map, user) => ...map, ...user.name: user, )
const sectorMap = sector.reduce((map, sec) => ...map, ...sector.comitID: sec), )
const data = invoices.map(invoice => ...invoice, ...userMap[invoice.name], ...sector[userMap[invoice.name].comitID])
add a comment |
I would suggest that you just iterate over the invoices and enrich the entries with the ones from the sets with unique entries.
Something like this (does not tested the code, but I hope you can understand the idea)
const data = invoices
.map(entry => ...entry, ...
users.find(user => user.name === entry.name)
.map(user => ...user,
sector.find(sec=> sec.comitID === user.commitID)
)
)
You could improve speed when you first create a map out of the sets and then just lookup the join attributes instead of searching for them
const userMap = users.reduce((map, user) => ...map, ...user.name: user, )
const sectorMap = sector.reduce((map, sec) => ...map, ...sector.comitID: sec), )
const data = invoices.map(invoice => ...invoice, ...userMap[invoice.name], ...sector[userMap[invoice.name].comitID])
I would suggest that you just iterate over the invoices and enrich the entries with the ones from the sets with unique entries.
Something like this (does not tested the code, but I hope you can understand the idea)
const data = invoices
.map(entry => ...entry, ...
users.find(user => user.name === entry.name)
.map(user => ...user,
sector.find(sec=> sec.comitID === user.commitID)
)
)
You could improve speed when you first create a map out of the sets and then just lookup the join attributes instead of searching for them
const userMap = users.reduce((map, user) => ...map, ...user.name: user, )
const sectorMap = sector.reduce((map, sec) => ...map, ...sector.comitID: sec), )
const data = invoices.map(invoice => ...invoice, ...userMap[invoice.name], ...sector[userMap[invoice.name].comitID])
edited Mar 8 at 20:32
answered Mar 8 at 20:26
thopawthopaw
1,340714
1,340714
add a comment |
add a comment |
Here's a basic script that could work.
const users = [ // Unique Entries
name:'user1', comitId: 'aa1',
name:'user2', comitId: 'aa2'
]
const sectors = [ // Unique Entries
comitID: 'aa1', department: 'finance',
comitID: 'aa2', department: 'marketing',
comitID: 'aa3', department: 'engineering'
]
const invoices = [ // Multiple Entries
name: 'user1', statementDate: '2/1/2019',
name: 'user1', statementDate: '2/14/2019',
name: 'user2', statementDate: '2/1/2019'
]
sectors.forEach(sector =>
const user = users.find(user => sector.comitID === user.comitId);
if (user)
user.department = sector.department;
);
const results = invoices.map(invoice =>
const user = users.find(user => invoice.name === user.name);
return Object.assign(, user, statementDate: invoice.statementDate );
);
console.log(results);
add a comment |
Here's a basic script that could work.
const users = [ // Unique Entries
name:'user1', comitId: 'aa1',
name:'user2', comitId: 'aa2'
]
const sectors = [ // Unique Entries
comitID: 'aa1', department: 'finance',
comitID: 'aa2', department: 'marketing',
comitID: 'aa3', department: 'engineering'
]
const invoices = [ // Multiple Entries
name: 'user1', statementDate: '2/1/2019',
name: 'user1', statementDate: '2/14/2019',
name: 'user2', statementDate: '2/1/2019'
]
sectors.forEach(sector =>
const user = users.find(user => sector.comitID === user.comitId);
if (user)
user.department = sector.department;
);
const results = invoices.map(invoice =>
const user = users.find(user => invoice.name === user.name);
return Object.assign(, user, statementDate: invoice.statementDate );
);
console.log(results);
add a comment |
Here's a basic script that could work.
const users = [ // Unique Entries
name:'user1', comitId: 'aa1',
name:'user2', comitId: 'aa2'
]
const sectors = [ // Unique Entries
comitID: 'aa1', department: 'finance',
comitID: 'aa2', department: 'marketing',
comitID: 'aa3', department: 'engineering'
]
const invoices = [ // Multiple Entries
name: 'user1', statementDate: '2/1/2019',
name: 'user1', statementDate: '2/14/2019',
name: 'user2', statementDate: '2/1/2019'
]
sectors.forEach(sector =>
const user = users.find(user => sector.comitID === user.comitId);
if (user)
user.department = sector.department;
);
const results = invoices.map(invoice =>
const user = users.find(user => invoice.name === user.name);
return Object.assign(, user, statementDate: invoice.statementDate );
);
console.log(results);
Here's a basic script that could work.
const users = [ // Unique Entries
name:'user1', comitId: 'aa1',
name:'user2', comitId: 'aa2'
]
const sectors = [ // Unique Entries
comitID: 'aa1', department: 'finance',
comitID: 'aa2', department: 'marketing',
comitID: 'aa3', department: 'engineering'
]
const invoices = [ // Multiple Entries
name: 'user1', statementDate: '2/1/2019',
name: 'user1', statementDate: '2/14/2019',
name: 'user2', statementDate: '2/1/2019'
]
sectors.forEach(sector =>
const user = users.find(user => sector.comitID === user.comitId);
if (user)
user.department = sector.department;
);
const results = invoices.map(invoice =>
const user = users.find(user => invoice.name === user.name);
return Object.assign(, user, statementDate: invoice.statementDate );
);
console.log(results);
answered Mar 8 at 20:32
bennettbuchananbennettbuchanan
162
162
add a comment |
add a comment |
You can use map & filter
const users = [ // Unique Entries
name: 'user1',
comitId: 'aa1'
,
name: 'user2',
comitId: 'aa2'
]
const sector = [ // Unique Entries
comitID: 'aa1',
department: 'finance'
,
comitID: 'aa2',
department: 'marketing'
,
comitID: 'aa3',
department: 'engineering'
]
const invoices = [ // Multiple Entries
name: 'user1',
statementDate: '2/1/2019'
,
name: 'user1',
statementDate: '2/14/2019'
,
name: 'user2',
statementDate: '2/1/2019'
]
let newArray = invoices.map(function(item)
// this value will be use to find match between users & sectors
let cId = users.filter(user => user.name === item.name)[0].comitId;
return
name: item.name,
statementDate: item.statementDate,
comitId: cId,
department: sector.filter(sector => sector.comitID === cId)[0].department
);
console.log(newArray)add a comment |
You can use map & filter
const users = [ // Unique Entries
name: 'user1',
comitId: 'aa1'
,
name: 'user2',
comitId: 'aa2'
]
const sector = [ // Unique Entries
comitID: 'aa1',
department: 'finance'
,
comitID: 'aa2',
department: 'marketing'
,
comitID: 'aa3',
department: 'engineering'
]
const invoices = [ // Multiple Entries
name: 'user1',
statementDate: '2/1/2019'
,
name: 'user1',
statementDate: '2/14/2019'
,
name: 'user2',
statementDate: '2/1/2019'
]
let newArray = invoices.map(function(item)
// this value will be use to find match between users & sectors
let cId = users.filter(user => user.name === item.name)[0].comitId;
return
name: item.name,
statementDate: item.statementDate,
comitId: cId,
department: sector.filter(sector => sector.comitID === cId)[0].department
);
console.log(newArray)add a comment |
You can use map & filter
const users = [ // Unique Entries
name: 'user1',
comitId: 'aa1'
,
name: 'user2',
comitId: 'aa2'
]
const sector = [ // Unique Entries
comitID: 'aa1',
department: 'finance'
,
comitID: 'aa2',
department: 'marketing'
,
comitID: 'aa3',
department: 'engineering'
]
const invoices = [ // Multiple Entries
name: 'user1',
statementDate: '2/1/2019'
,
name: 'user1',
statementDate: '2/14/2019'
,
name: 'user2',
statementDate: '2/1/2019'
]
let newArray = invoices.map(function(item)
// this value will be use to find match between users & sectors
let cId = users.filter(user => user.name === item.name)[0].comitId;
return
name: item.name,
statementDate: item.statementDate,
comitId: cId,
department: sector.filter(sector => sector.comitID === cId)[0].department
);
console.log(newArray)You can use map & filter
const users = [ // Unique Entries
name: 'user1',
comitId: 'aa1'
,
name: 'user2',
comitId: 'aa2'
]
const sector = [ // Unique Entries
comitID: 'aa1',
department: 'finance'
,
comitID: 'aa2',
department: 'marketing'
,
comitID: 'aa3',
department: 'engineering'
]
const invoices = [ // Multiple Entries
name: 'user1',
statementDate: '2/1/2019'
,
name: 'user1',
statementDate: '2/14/2019'
,
name: 'user2',
statementDate: '2/1/2019'
]
let newArray = invoices.map(function(item)
// this value will be use to find match between users & sectors
let cId = users.filter(user => user.name === item.name)[0].comitId;
return
name: item.name,
statementDate: item.statementDate,
comitId: cId,
department: sector.filter(sector => sector.comitID === cId)[0].department
);
console.log(newArray)const users = [ // Unique Entries
name: 'user1',
comitId: 'aa1'
,
name: 'user2',
comitId: 'aa2'
]
const sector = [ // Unique Entries
comitID: 'aa1',
department: 'finance'
,
comitID: 'aa2',
department: 'marketing'
,
comitID: 'aa3',
department: 'engineering'
]
const invoices = [ // Multiple Entries
name: 'user1',
statementDate: '2/1/2019'
,
name: 'user1',
statementDate: '2/14/2019'
,
name: 'user2',
statementDate: '2/1/2019'
]
let newArray = invoices.map(function(item)
// this value will be use to find match between users & sectors
let cId = users.filter(user => user.name === item.name)[0].comitId;
return
name: item.name,
statementDate: item.statementDate,
comitId: cId,
department: sector.filter(sector => sector.comitID === cId)[0].department
);
console.log(newArray)const users = [ // Unique Entries
name: 'user1',
comitId: 'aa1'
,
name: 'user2',
comitId: 'aa2'
]
const sector = [ // Unique Entries
comitID: 'aa1',
department: 'finance'
,
comitID: 'aa2',
department: 'marketing'
,
comitID: 'aa3',
department: 'engineering'
]
const invoices = [ // Multiple Entries
name: 'user1',
statementDate: '2/1/2019'
,
name: 'user1',
statementDate: '2/14/2019'
,
name: 'user2',
statementDate: '2/1/2019'
]
let newArray = invoices.map(function(item)
// this value will be use to find match between users & sectors
let cId = users.filter(user => user.name === item.name)[0].comitId;
return
name: item.name,
statementDate: item.statementDate,
comitId: cId,
department: sector.filter(sector => sector.comitID === cId)[0].department
);
console.log(newArray)edited Mar 8 at 20:39
answered Mar 8 at 20:33
brkbrk
29.7k32244
29.7k32244
add a comment |
add a comment |
You could move user and sector items into a map and take this object, if necessary.
const
users = [ name: 'user1', comitId: 'aa1' , name: 'user2', comitId: 'aa2' ],
sector = [ comitID: 'aa1', department: 'finance' , comitID: 'aa2', department: 'marketing' , comitID: 'aa3', department: 'engineering' ],
invoices = [ name: 'user1', statementDate: '2/1/2019', name: 'user1', statementDate: '2/14/2019' , name: 'user2', statementDate: '2/1/2019' ],
setMap = k => (m, o) => m.set(o[k], o),
userMap = users.reduce(setMap('name'), new Map),
sectorMap = sector.reduce(setMap('comitID'), new Map),
result = invoices.map(( name, statementDate ) =>
var comitId = userMap.get(name),
department = sectorMap.get(comitId);
return name, comitId, department, statementDate ;
);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; add a comment |
You could move user and sector items into a map and take this object, if necessary.
const
users = [ name: 'user1', comitId: 'aa1' , name: 'user2', comitId: 'aa2' ],
sector = [ comitID: 'aa1', department: 'finance' , comitID: 'aa2', department: 'marketing' , comitID: 'aa3', department: 'engineering' ],
invoices = [ name: 'user1', statementDate: '2/1/2019', name: 'user1', statementDate: '2/14/2019' , name: 'user2', statementDate: '2/1/2019' ],
setMap = k => (m, o) => m.set(o[k], o),
userMap = users.reduce(setMap('name'), new Map),
sectorMap = sector.reduce(setMap('comitID'), new Map),
result = invoices.map(( name, statementDate ) =>
var comitId = userMap.get(name),
department = sectorMap.get(comitId);
return name, comitId, department, statementDate ;
);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; add a comment |
You could move user and sector items into a map and take this object, if necessary.
const
users = [ name: 'user1', comitId: 'aa1' , name: 'user2', comitId: 'aa2' ],
sector = [ comitID: 'aa1', department: 'finance' , comitID: 'aa2', department: 'marketing' , comitID: 'aa3', department: 'engineering' ],
invoices = [ name: 'user1', statementDate: '2/1/2019', name: 'user1', statementDate: '2/14/2019' , name: 'user2', statementDate: '2/1/2019' ],
setMap = k => (m, o) => m.set(o[k], o),
userMap = users.reduce(setMap('name'), new Map),
sectorMap = sector.reduce(setMap('comitID'), new Map),
result = invoices.map(( name, statementDate ) =>
var comitId = userMap.get(name),
department = sectorMap.get(comitId);
return name, comitId, department, statementDate ;
);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; You could move user and sector items into a map and take this object, if necessary.
const
users = [ name: 'user1', comitId: 'aa1' , name: 'user2', comitId: 'aa2' ],
sector = [ comitID: 'aa1', department: 'finance' , comitID: 'aa2', department: 'marketing' , comitID: 'aa3', department: 'engineering' ],
invoices = [ name: 'user1', statementDate: '2/1/2019', name: 'user1', statementDate: '2/14/2019' , name: 'user2', statementDate: '2/1/2019' ],
setMap = k => (m, o) => m.set(o[k], o),
userMap = users.reduce(setMap('name'), new Map),
sectorMap = sector.reduce(setMap('comitID'), new Map),
result = invoices.map(( name, statementDate ) =>
var comitId = userMap.get(name),
department = sectorMap.get(comitId);
return name, comitId, department, statementDate ;
);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; const
users = [ name: 'user1', comitId: 'aa1' , name: 'user2', comitId: 'aa2' ],
sector = [ comitID: 'aa1', department: 'finance' , comitID: 'aa2', department: 'marketing' , comitID: 'aa3', department: 'engineering' ],
invoices = [ name: 'user1', statementDate: '2/1/2019', name: 'user1', statementDate: '2/14/2019' , name: 'user2', statementDate: '2/1/2019' ],
setMap = k => (m, o) => m.set(o[k], o),
userMap = users.reduce(setMap('name'), new Map),
sectorMap = sector.reduce(setMap('comitID'), new Map),
result = invoices.map(( name, statementDate ) =>
var comitId = userMap.get(name),
department = sectorMap.get(comitId);
return name, comitId, department, statementDate ;
);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; const
users = [ name: 'user1', comitId: 'aa1' , name: 'user2', comitId: 'aa2' ],
sector = [ comitID: 'aa1', department: 'finance' , comitID: 'aa2', department: 'marketing' , comitID: 'aa3', department: 'engineering' ],
invoices = [ name: 'user1', statementDate: '2/1/2019', name: 'user1', statementDate: '2/14/2019' , name: 'user2', statementDate: '2/1/2019' ],
setMap = k => (m, o) => m.set(o[k], o),
userMap = users.reduce(setMap('name'), new Map),
sectorMap = sector.reduce(setMap('comitID'), new Map),
result = invoices.map(( name, statementDate ) =>
var comitId = userMap.get(name),
department = sectorMap.get(comitId);
return name, comitId, department, statementDate ;
);
console.log(result);.as-console-wrapper max-height: 100% !important; top: 0; edited Mar 8 at 20:42
answered Mar 8 at 20:32
Nina ScholzNina Scholz
195k15107179
195k15107179
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55070403%2fmap-3-arrays-of-objects-by-key-in-javascript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
do you have anything you have tried? This really does look like something you would solve in excel or sql.
– SpeedOfRound
Mar 8 at 20:22
Here's a similar question that might help: Analog to SQL 'JOIN' for Javascript objects?
– nireno
Mar 8 at 22:21