Reduce() an object of an element that has already been reduced
I was doing some data manipulation, but I made a mistake and got the wrong data structure, so initially my data is: (link to data)
const employees = [{
"EmployeeID": "100A",
"FirstName": "Downs",
"aval": [
{"start": "11-19", "end": "2", "ava": "30", "health": "4"},
{"start": "11-20", "end": "2", "ava": "40", "health": "4"},
{"start": "11-21", "end": "2", "ava": "50", "health": "4"},
{"start": "11-22", "end": "2", "ava": "60", "health": "4"}
]
},
{
"EmployeeID": "100B",
"FirstName": "Mckenzie",
"aval": [
{"start": "11-19", "end": "2", "ava": "1", "health": "4"},
{"start": "11-20", "end": "2", "ava": "2", "health": "4"},
{"start": "11-21", "end": "2", "ava": "3", "health": "4"},
{"start": "11-22", "end": "2", "ava": "4", "health": "4"}
]
},
]
and would like to get it like:
const employees = [
{ "EmployeeID": "100A", "11-19": "30", "11-20": "40", "11-21": "50", "11-22": "60"},
{ "EmployeeID": "100B", "11-19": "1", "11-20": "2", "11-21": "3", "11-22": "4"}
]
I've asked around and this gives me 10 objects instead of two, just need to put those pesky employeeID inside 1 object with all the start values altogether.
const res = employees.reduce((acc, { EmployeeID, aval}) => [
...acc,
...aval.map( ({ start, ava}) => ({ EmployeeID, [start]: ava}) )
], );
javascript arrays angular object reduce
add a comment |
I was doing some data manipulation, but I made a mistake and got the wrong data structure, so initially my data is: (link to data)
const employees = [{
"EmployeeID": "100A",
"FirstName": "Downs",
"aval": [
{"start": "11-19", "end": "2", "ava": "30", "health": "4"},
{"start": "11-20", "end": "2", "ava": "40", "health": "4"},
{"start": "11-21", "end": "2", "ava": "50", "health": "4"},
{"start": "11-22", "end": "2", "ava": "60", "health": "4"}
]
},
{
"EmployeeID": "100B",
"FirstName": "Mckenzie",
"aval": [
{"start": "11-19", "end": "2", "ava": "1", "health": "4"},
{"start": "11-20", "end": "2", "ava": "2", "health": "4"},
{"start": "11-21", "end": "2", "ava": "3", "health": "4"},
{"start": "11-22", "end": "2", "ava": "4", "health": "4"}
]
},
]
and would like to get it like:
const employees = [
{ "EmployeeID": "100A", "11-19": "30", "11-20": "40", "11-21": "50", "11-22": "60"},
{ "EmployeeID": "100B", "11-19": "1", "11-20": "2", "11-21": "3", "11-22": "4"}
]
I've asked around and this gives me 10 objects instead of two, just need to put those pesky employeeID inside 1 object with all the start values altogether.
const res = employees.reduce((acc, { EmployeeID, aval}) => [
...acc,
...aval.map( ({ start, ava}) => ({ EmployeeID, [start]: ava}) )
], );
javascript arrays angular object reduce
add a comment |
I was doing some data manipulation, but I made a mistake and got the wrong data structure, so initially my data is: (link to data)
const employees = [{
"EmployeeID": "100A",
"FirstName": "Downs",
"aval": [
{"start": "11-19", "end": "2", "ava": "30", "health": "4"},
{"start": "11-20", "end": "2", "ava": "40", "health": "4"},
{"start": "11-21", "end": "2", "ava": "50", "health": "4"},
{"start": "11-22", "end": "2", "ava": "60", "health": "4"}
]
},
{
"EmployeeID": "100B",
"FirstName": "Mckenzie",
"aval": [
{"start": "11-19", "end": "2", "ava": "1", "health": "4"},
{"start": "11-20", "end": "2", "ava": "2", "health": "4"},
{"start": "11-21", "end": "2", "ava": "3", "health": "4"},
{"start": "11-22", "end": "2", "ava": "4", "health": "4"}
]
},
]
and would like to get it like:
const employees = [
{ "EmployeeID": "100A", "11-19": "30", "11-20": "40", "11-21": "50", "11-22": "60"},
{ "EmployeeID": "100B", "11-19": "1", "11-20": "2", "11-21": "3", "11-22": "4"}
]
I've asked around and this gives me 10 objects instead of two, just need to put those pesky employeeID inside 1 object with all the start values altogether.
const res = employees.reduce((acc, { EmployeeID, aval}) => [
...acc,
...aval.map( ({ start, ava}) => ({ EmployeeID, [start]: ava}) )
], );
javascript arrays angular object reduce
I was doing some data manipulation, but I made a mistake and got the wrong data structure, so initially my data is: (link to data)
const employees = [{
"EmployeeID": "100A",
"FirstName": "Downs",
"aval": [
{"start": "11-19", "end": "2", "ava": "30", "health": "4"},
{"start": "11-20", "end": "2", "ava": "40", "health": "4"},
{"start": "11-21", "end": "2", "ava": "50", "health": "4"},
{"start": "11-22", "end": "2", "ava": "60", "health": "4"}
]
},
{
"EmployeeID": "100B",
"FirstName": "Mckenzie",
"aval": [
{"start": "11-19", "end": "2", "ava": "1", "health": "4"},
{"start": "11-20", "end": "2", "ava": "2", "health": "4"},
{"start": "11-21", "end": "2", "ava": "3", "health": "4"},
{"start": "11-22", "end": "2", "ava": "4", "health": "4"}
]
},
]
and would like to get it like:
const employees = [
{ "EmployeeID": "100A", "11-19": "30", "11-20": "40", "11-21": "50", "11-22": "60"},
{ "EmployeeID": "100B", "11-19": "1", "11-20": "2", "11-21": "3", "11-22": "4"}
]
I've asked around and this gives me 10 objects instead of two, just need to put those pesky employeeID inside 1 object with all the start values altogether.
const res = employees.reduce((acc, { EmployeeID, aval}) => [
...acc,
...aval.map( ({ start, ava}) => ({ EmployeeID, [start]: ava}) )
], );
javascript arrays angular object reduce
javascript arrays angular object reduce
asked Nov 24 '18 at 22:50
brohymnbrohymn
157114
157114
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Because your input items and output items are one-to-one, you should use .map
on employees
instead of .reduce
:
const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];
const res = employees.map(({ EmployeeID, aval }) => (
Object.assign(
{ EmployeeID },
...aval.map(({ start, ava }) => ({ [start]: ava }))
)
));
console.log(res);
Or, if you wanted to create fewer discarded intermediate objects, you could reduce
the inner array aval
instead of .map
:
const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];
const res = employees.map(({ EmployeeID, aval }) => (
aval.reduce((a, { start, ava }) => {
a[start] = ava;
return a;
}, { EmployeeID })
));
console.log(res);
Nice, thanks. any recommendations to learn reduce besides or whacky data structures besides the MDN site ?
– brohymn
Nov 24 '18 at 23:07
Learn reduce, as in, when to use it, or how to use it?
– CertainPerformance
Nov 24 '18 at 23:34
as in , how and when to implement it?, maybe practice it. it seems like one of the subjects I have the most trouble with. the MDN definition doesnt really help
– brohymn
Nov 24 '18 at 23:36
1
When to use: When you need to transform an array into another value, such as a primitive, an object, or another array. Only usereduce
if neither.map
nor.filter
are appropriate. How: when reducing into a primitive, usually you can use the sort of snippets given on MDN, eg(a, b) => a + b
, just concatenate/add/multiply/etc the accumulator with the next item in the array. But often it's a bit more complicated, like when reducing into an object. The patterna[prop] = val; return a;
is very common. You might search SO for answers by high-rep users containingreduce
for examples
– CertainPerformance
Nov 24 '18 at 23:42
When you need to transform an array into a value (or multiple values), you can almost always use.reduce
if the other transformation methods (forEach excluded) aren't suitable, feel free to hit me up in chat for details
– CertainPerformance
Nov 24 '18 at 23:44
|
show 2 more comments
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%2f53463041%2freduce-an-object-of-an-element-that-has-already-been-reduced%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Because your input items and output items are one-to-one, you should use .map
on employees
instead of .reduce
:
const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];
const res = employees.map(({ EmployeeID, aval }) => (
Object.assign(
{ EmployeeID },
...aval.map(({ start, ava }) => ({ [start]: ava }))
)
));
console.log(res);
Or, if you wanted to create fewer discarded intermediate objects, you could reduce
the inner array aval
instead of .map
:
const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];
const res = employees.map(({ EmployeeID, aval }) => (
aval.reduce((a, { start, ava }) => {
a[start] = ava;
return a;
}, { EmployeeID })
));
console.log(res);
Nice, thanks. any recommendations to learn reduce besides or whacky data structures besides the MDN site ?
– brohymn
Nov 24 '18 at 23:07
Learn reduce, as in, when to use it, or how to use it?
– CertainPerformance
Nov 24 '18 at 23:34
as in , how and when to implement it?, maybe practice it. it seems like one of the subjects I have the most trouble with. the MDN definition doesnt really help
– brohymn
Nov 24 '18 at 23:36
1
When to use: When you need to transform an array into another value, such as a primitive, an object, or another array. Only usereduce
if neither.map
nor.filter
are appropriate. How: when reducing into a primitive, usually you can use the sort of snippets given on MDN, eg(a, b) => a + b
, just concatenate/add/multiply/etc the accumulator with the next item in the array. But often it's a bit more complicated, like when reducing into an object. The patterna[prop] = val; return a;
is very common. You might search SO for answers by high-rep users containingreduce
for examples
– CertainPerformance
Nov 24 '18 at 23:42
When you need to transform an array into a value (or multiple values), you can almost always use.reduce
if the other transformation methods (forEach excluded) aren't suitable, feel free to hit me up in chat for details
– CertainPerformance
Nov 24 '18 at 23:44
|
show 2 more comments
Because your input items and output items are one-to-one, you should use .map
on employees
instead of .reduce
:
const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];
const res = employees.map(({ EmployeeID, aval }) => (
Object.assign(
{ EmployeeID },
...aval.map(({ start, ava }) => ({ [start]: ava }))
)
));
console.log(res);
Or, if you wanted to create fewer discarded intermediate objects, you could reduce
the inner array aval
instead of .map
:
const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];
const res = employees.map(({ EmployeeID, aval }) => (
aval.reduce((a, { start, ava }) => {
a[start] = ava;
return a;
}, { EmployeeID })
));
console.log(res);
Nice, thanks. any recommendations to learn reduce besides or whacky data structures besides the MDN site ?
– brohymn
Nov 24 '18 at 23:07
Learn reduce, as in, when to use it, or how to use it?
– CertainPerformance
Nov 24 '18 at 23:34
as in , how and when to implement it?, maybe practice it. it seems like one of the subjects I have the most trouble with. the MDN definition doesnt really help
– brohymn
Nov 24 '18 at 23:36
1
When to use: When you need to transform an array into another value, such as a primitive, an object, or another array. Only usereduce
if neither.map
nor.filter
are appropriate. How: when reducing into a primitive, usually you can use the sort of snippets given on MDN, eg(a, b) => a + b
, just concatenate/add/multiply/etc the accumulator with the next item in the array. But often it's a bit more complicated, like when reducing into an object. The patterna[prop] = val; return a;
is very common. You might search SO for answers by high-rep users containingreduce
for examples
– CertainPerformance
Nov 24 '18 at 23:42
When you need to transform an array into a value (or multiple values), you can almost always use.reduce
if the other transformation methods (forEach excluded) aren't suitable, feel free to hit me up in chat for details
– CertainPerformance
Nov 24 '18 at 23:44
|
show 2 more comments
Because your input items and output items are one-to-one, you should use .map
on employees
instead of .reduce
:
const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];
const res = employees.map(({ EmployeeID, aval }) => (
Object.assign(
{ EmployeeID },
...aval.map(({ start, ava }) => ({ [start]: ava }))
)
));
console.log(res);
Or, if you wanted to create fewer discarded intermediate objects, you could reduce
the inner array aval
instead of .map
:
const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];
const res = employees.map(({ EmployeeID, aval }) => (
aval.reduce((a, { start, ava }) => {
a[start] = ava;
return a;
}, { EmployeeID })
));
console.log(res);
Because your input items and output items are one-to-one, you should use .map
on employees
instead of .reduce
:
const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];
const res = employees.map(({ EmployeeID, aval }) => (
Object.assign(
{ EmployeeID },
...aval.map(({ start, ava }) => ({ [start]: ava }))
)
));
console.log(res);
Or, if you wanted to create fewer discarded intermediate objects, you could reduce
the inner array aval
instead of .map
:
const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];
const res = employees.map(({ EmployeeID, aval }) => (
aval.reduce((a, { start, ava }) => {
a[start] = ava;
return a;
}, { EmployeeID })
));
console.log(res);
const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];
const res = employees.map(({ EmployeeID, aval }) => (
Object.assign(
{ EmployeeID },
...aval.map(({ start, ava }) => ({ [start]: ava }))
)
));
console.log(res);
const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];
const res = employees.map(({ EmployeeID, aval }) => (
Object.assign(
{ EmployeeID },
...aval.map(({ start, ava }) => ({ [start]: ava }))
)
));
console.log(res);
const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];
const res = employees.map(({ EmployeeID, aval }) => (
aval.reduce((a, { start, ava }) => {
a[start] = ava;
return a;
}, { EmployeeID })
));
console.log(res);
const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];
const res = employees.map(({ EmployeeID, aval }) => (
aval.reduce((a, { start, ava }) => {
a[start] = ava;
return a;
}, { EmployeeID })
));
console.log(res);
edited Nov 24 '18 at 23:02
answered Nov 24 '18 at 22:56
CertainPerformanceCertainPerformance
81.5k143966
81.5k143966
Nice, thanks. any recommendations to learn reduce besides or whacky data structures besides the MDN site ?
– brohymn
Nov 24 '18 at 23:07
Learn reduce, as in, when to use it, or how to use it?
– CertainPerformance
Nov 24 '18 at 23:34
as in , how and when to implement it?, maybe practice it. it seems like one of the subjects I have the most trouble with. the MDN definition doesnt really help
– brohymn
Nov 24 '18 at 23:36
1
When to use: When you need to transform an array into another value, such as a primitive, an object, or another array. Only usereduce
if neither.map
nor.filter
are appropriate. How: when reducing into a primitive, usually you can use the sort of snippets given on MDN, eg(a, b) => a + b
, just concatenate/add/multiply/etc the accumulator with the next item in the array. But often it's a bit more complicated, like when reducing into an object. The patterna[prop] = val; return a;
is very common. You might search SO for answers by high-rep users containingreduce
for examples
– CertainPerformance
Nov 24 '18 at 23:42
When you need to transform an array into a value (or multiple values), you can almost always use.reduce
if the other transformation methods (forEach excluded) aren't suitable, feel free to hit me up in chat for details
– CertainPerformance
Nov 24 '18 at 23:44
|
show 2 more comments
Nice, thanks. any recommendations to learn reduce besides or whacky data structures besides the MDN site ?
– brohymn
Nov 24 '18 at 23:07
Learn reduce, as in, when to use it, or how to use it?
– CertainPerformance
Nov 24 '18 at 23:34
as in , how and when to implement it?, maybe practice it. it seems like one of the subjects I have the most trouble with. the MDN definition doesnt really help
– brohymn
Nov 24 '18 at 23:36
1
When to use: When you need to transform an array into another value, such as a primitive, an object, or another array. Only usereduce
if neither.map
nor.filter
are appropriate. How: when reducing into a primitive, usually you can use the sort of snippets given on MDN, eg(a, b) => a + b
, just concatenate/add/multiply/etc the accumulator with the next item in the array. But often it's a bit more complicated, like when reducing into an object. The patterna[prop] = val; return a;
is very common. You might search SO for answers by high-rep users containingreduce
for examples
– CertainPerformance
Nov 24 '18 at 23:42
When you need to transform an array into a value (or multiple values), you can almost always use.reduce
if the other transformation methods (forEach excluded) aren't suitable, feel free to hit me up in chat for details
– CertainPerformance
Nov 24 '18 at 23:44
Nice, thanks. any recommendations to learn reduce besides or whacky data structures besides the MDN site ?
– brohymn
Nov 24 '18 at 23:07
Nice, thanks. any recommendations to learn reduce besides or whacky data structures besides the MDN site ?
– brohymn
Nov 24 '18 at 23:07
Learn reduce, as in, when to use it, or how to use it?
– CertainPerformance
Nov 24 '18 at 23:34
Learn reduce, as in, when to use it, or how to use it?
– CertainPerformance
Nov 24 '18 at 23:34
as in , how and when to implement it?, maybe practice it. it seems like one of the subjects I have the most trouble with. the MDN definition doesnt really help
– brohymn
Nov 24 '18 at 23:36
as in , how and when to implement it?, maybe practice it. it seems like one of the subjects I have the most trouble with. the MDN definition doesnt really help
– brohymn
Nov 24 '18 at 23:36
1
1
When to use: When you need to transform an array into another value, such as a primitive, an object, or another array. Only use
reduce
if neither .map
nor .filter
are appropriate. How: when reducing into a primitive, usually you can use the sort of snippets given on MDN, eg (a, b) => a + b
, just concatenate/add/multiply/etc the accumulator with the next item in the array. But often it's a bit more complicated, like when reducing into an object. The pattern a[prop] = val; return a;
is very common. You might search SO for answers by high-rep users containing reduce
for examples– CertainPerformance
Nov 24 '18 at 23:42
When to use: When you need to transform an array into another value, such as a primitive, an object, or another array. Only use
reduce
if neither .map
nor .filter
are appropriate. How: when reducing into a primitive, usually you can use the sort of snippets given on MDN, eg (a, b) => a + b
, just concatenate/add/multiply/etc the accumulator with the next item in the array. But often it's a bit more complicated, like when reducing into an object. The pattern a[prop] = val; return a;
is very common. You might search SO for answers by high-rep users containing reduce
for examples– CertainPerformance
Nov 24 '18 at 23:42
When you need to transform an array into a value (or multiple values), you can almost always use
.reduce
if the other transformation methods (forEach excluded) aren't suitable, feel free to hit me up in chat for details– CertainPerformance
Nov 24 '18 at 23:44
When you need to transform an array into a value (or multiple values), you can almost always use
.reduce
if the other transformation methods (forEach excluded) aren't suitable, feel free to hit me up in chat for details– CertainPerformance
Nov 24 '18 at 23:44
|
show 2 more comments
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%2f53463041%2freduce-an-object-of-an-element-that-has-already-been-reduced%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