Get nested object values and put it on a table header
In the following stackblitz sample. I have the following data:
My table is displaying what I want, As you can see inside the timeline object the "start" values are exactly the same as the ones inside the aval array
Therefore, is there anyway I can display those table headers without the timeline object? (using filter, reduce or Array.from) Maybe I can loop to that very nested aval array and get them as an individual array kinda like
"aval" = ["11-19", "11-20", "11-21", "11-22"]
I'm using filter but getting nowhere so far.
DATA:
{
"timeline": {
"timeline_values": [{
"tag": 0,
"start": "11-19"
},
{
"tag": 1,
"start": "11-20"
},
{
"tag": 2,
"start": "11-21"
},
{
"tag": 3,
"start": "11-22"
}
]
},
"employees": [{
"EmployeeID": "56250f",
"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"
}
]
},
javascript arrays angular reduce
add a comment |
In the following stackblitz sample. I have the following data:
My table is displaying what I want, As you can see inside the timeline object the "start" values are exactly the same as the ones inside the aval array
Therefore, is there anyway I can display those table headers without the timeline object? (using filter, reduce or Array.from) Maybe I can loop to that very nested aval array and get them as an individual array kinda like
"aval" = ["11-19", "11-20", "11-21", "11-22"]
I'm using filter but getting nowhere so far.
DATA:
{
"timeline": {
"timeline_values": [{
"tag": 0,
"start": "11-19"
},
{
"tag": 1,
"start": "11-20"
},
{
"tag": 2,
"start": "11-21"
},
{
"tag": 3,
"start": "11-22"
}
]
},
"employees": [{
"EmployeeID": "56250f",
"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"
}
]
},
javascript arrays angular reduce
Can you upload the full data and format it?
– John Kennedy
Nov 23 at 0:03
Is your question how to create an array calledheaders
like["11-19", "11-20", "11-21", "11-22"]
from theemployees.aval
array in the data you posted?
– slider
Nov 23 at 0:08
data is inside the grid.component.ts the variable is called this.localObject , please open the stackblitz
– brohymn
Nov 23 at 0:10
add a comment |
In the following stackblitz sample. I have the following data:
My table is displaying what I want, As you can see inside the timeline object the "start" values are exactly the same as the ones inside the aval array
Therefore, is there anyway I can display those table headers without the timeline object? (using filter, reduce or Array.from) Maybe I can loop to that very nested aval array and get them as an individual array kinda like
"aval" = ["11-19", "11-20", "11-21", "11-22"]
I'm using filter but getting nowhere so far.
DATA:
{
"timeline": {
"timeline_values": [{
"tag": 0,
"start": "11-19"
},
{
"tag": 1,
"start": "11-20"
},
{
"tag": 2,
"start": "11-21"
},
{
"tag": 3,
"start": "11-22"
}
]
},
"employees": [{
"EmployeeID": "56250f",
"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"
}
]
},
javascript arrays angular reduce
In the following stackblitz sample. I have the following data:
My table is displaying what I want, As you can see inside the timeline object the "start" values are exactly the same as the ones inside the aval array
Therefore, is there anyway I can display those table headers without the timeline object? (using filter, reduce or Array.from) Maybe I can loop to that very nested aval array and get them as an individual array kinda like
"aval" = ["11-19", "11-20", "11-21", "11-22"]
I'm using filter but getting nowhere so far.
DATA:
{
"timeline": {
"timeline_values": [{
"tag": 0,
"start": "11-19"
},
{
"tag": 1,
"start": "11-20"
},
{
"tag": 2,
"start": "11-21"
},
{
"tag": 3,
"start": "11-22"
}
]
},
"employees": [{
"EmployeeID": "56250f",
"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"
}
]
},
javascript arrays angular reduce
javascript arrays angular reduce
asked Nov 22 at 23:56
brohymn
157113
157113
Can you upload the full data and format it?
– John Kennedy
Nov 23 at 0:03
Is your question how to create an array calledheaders
like["11-19", "11-20", "11-21", "11-22"]
from theemployees.aval
array in the data you posted?
– slider
Nov 23 at 0:08
data is inside the grid.component.ts the variable is called this.localObject , please open the stackblitz
– brohymn
Nov 23 at 0:10
add a comment |
Can you upload the full data and format it?
– John Kennedy
Nov 23 at 0:03
Is your question how to create an array calledheaders
like["11-19", "11-20", "11-21", "11-22"]
from theemployees.aval
array in the data you posted?
– slider
Nov 23 at 0:08
data is inside the grid.component.ts the variable is called this.localObject , please open the stackblitz
– brohymn
Nov 23 at 0:10
Can you upload the full data and format it?
– John Kennedy
Nov 23 at 0:03
Can you upload the full data and format it?
– John Kennedy
Nov 23 at 0:03
Is your question how to create an array called
headers
like ["11-19", "11-20", "11-21", "11-22"]
from the employees.aval
array in the data you posted?– slider
Nov 23 at 0:08
Is your question how to create an array called
headers
like ["11-19", "11-20", "11-21", "11-22"]
from the employees.aval
array in the data you posted?– slider
Nov 23 at 0:08
data is inside the grid.component.ts the variable is called this.localObject , please open the stackblitz
– brohymn
Nov 23 at 0:10
data is inside the grid.component.ts the variable is called this.localObject , please open the stackblitz
– brohymn
Nov 23 at 0:10
add a comment |
2 Answers
2
active
oldest
votes
Assuming you wanted the aval list from employee list, you can try a combination of map
and reduce
Map to create arrays of array which will have all start aval values for a particular employee
employee
->aval
->start
Reduce to flatten the array of arrays
Finally use ES6
Set
to pick unique values.
const employees = [{
"EmployeeID": "56250f",
"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": "56250f",
"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"
}
]
}, {
"EmployeeID": "56250f",
"FirstName": "Charles",
"aval": [{
"start": "11-19",
"end": "2",
"ava": "100",
"health": "4"
},
{
"start": "11-20",
"end": "2",
"ava": "200",
"health": "4"
},
{
"start": "11-21",
"end": "2",
"ava": "300",
"health": "4"
},
{
"start": "11-22",
"end": "2",
"ava": "400",
"health": "4"
}
]
}
]
const mappedAvals = employees.map(emp => emp.aval.map(av => av.start));
console.log("************* MAPPED AVALS **************");
console.log(mappedAvals);
console.log("************* MAPPED AVALS **************");
const flattenedAvals = mappedAvals.reduce((acc, aval) => [...acc, ...aval], );
console.log("************* FLATTENED AVALS **************");
console.log(flattenedAvals);
console.log("************* FLATTENED AVALS **************");
const distinctAvals = [...(new Set(flattenedAvals))];
console.log("************* DISTINCT AVALS **************");
console.log(distinctAvals);
console.log("************* DISTINCT AVALS **************");
Thanks. Reduce is not my forte. may I ask a good source of learning ?
– brohymn
Nov 23 at 0:23
Trying out multiple examples like adding a list of numbers, counting the occurrences of characters, merging lists etc will definitely get you started but sorry, I am not aware of any particular good sources. The way you use the accumulator argument is the key to reduce method IMHO.
– Nandu Kalidindi
Nov 23 at 0:27
add a comment |
You can also use flatMap
(if it's supported) and a Set
:
const aval = [...new Set(employees.flatMap(e => e.aval.map(a => a.start)))];
const employees = [{
"EmployeeID": "56250f",
"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": "56250f",
"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"
}
]
}, {
"EmployeeID": "56250f",
"FirstName": "Charles",
"aval": [{
"start": "11-19",
"end": "2",
"ava": "100",
"health": "4"
},
{
"start": "11-20",
"end": "2",
"ava": "200",
"health": "4"
},
{
"start": "11-21",
"end": "2",
"ava": "300",
"health": "4"
},
{
"start": "11-22",
"end": "2",
"ava": "400",
"health": "4"
}
]
}
]
const aval = [...new Set(employees.flatMap(e => e.aval.map(a => a.start)))];
console.log(aval);
1
This solution is much cleaner and it works , however there is a couple of things to consider. First, in Angular/ typescript, I have to change the data type of employees from any to any, which sometimes cannot be ideal. Also according to mdn IE and EDGE don't support it. they provide an alternative with reduce and concat tho.
– brohymn
Nov 23 at 15:41
@brohymn as for your second point about IE and Edge, the Typescript you write in your angular app is transpiled to Javascript that should work in all browsers: angular.io/guide/typescript-configuration
– slider
Nov 23 at 15:53
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%2f53439224%2fget-nested-object-values-and-put-it-on-a-table-header%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assuming you wanted the aval list from employee list, you can try a combination of map
and reduce
Map to create arrays of array which will have all start aval values for a particular employee
employee
->aval
->start
Reduce to flatten the array of arrays
Finally use ES6
Set
to pick unique values.
const employees = [{
"EmployeeID": "56250f",
"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": "56250f",
"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"
}
]
}, {
"EmployeeID": "56250f",
"FirstName": "Charles",
"aval": [{
"start": "11-19",
"end": "2",
"ava": "100",
"health": "4"
},
{
"start": "11-20",
"end": "2",
"ava": "200",
"health": "4"
},
{
"start": "11-21",
"end": "2",
"ava": "300",
"health": "4"
},
{
"start": "11-22",
"end": "2",
"ava": "400",
"health": "4"
}
]
}
]
const mappedAvals = employees.map(emp => emp.aval.map(av => av.start));
console.log("************* MAPPED AVALS **************");
console.log(mappedAvals);
console.log("************* MAPPED AVALS **************");
const flattenedAvals = mappedAvals.reduce((acc, aval) => [...acc, ...aval], );
console.log("************* FLATTENED AVALS **************");
console.log(flattenedAvals);
console.log("************* FLATTENED AVALS **************");
const distinctAvals = [...(new Set(flattenedAvals))];
console.log("************* DISTINCT AVALS **************");
console.log(distinctAvals);
console.log("************* DISTINCT AVALS **************");
Thanks. Reduce is not my forte. may I ask a good source of learning ?
– brohymn
Nov 23 at 0:23
Trying out multiple examples like adding a list of numbers, counting the occurrences of characters, merging lists etc will definitely get you started but sorry, I am not aware of any particular good sources. The way you use the accumulator argument is the key to reduce method IMHO.
– Nandu Kalidindi
Nov 23 at 0:27
add a comment |
Assuming you wanted the aval list from employee list, you can try a combination of map
and reduce
Map to create arrays of array which will have all start aval values for a particular employee
employee
->aval
->start
Reduce to flatten the array of arrays
Finally use ES6
Set
to pick unique values.
const employees = [{
"EmployeeID": "56250f",
"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": "56250f",
"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"
}
]
}, {
"EmployeeID": "56250f",
"FirstName": "Charles",
"aval": [{
"start": "11-19",
"end": "2",
"ava": "100",
"health": "4"
},
{
"start": "11-20",
"end": "2",
"ava": "200",
"health": "4"
},
{
"start": "11-21",
"end": "2",
"ava": "300",
"health": "4"
},
{
"start": "11-22",
"end": "2",
"ava": "400",
"health": "4"
}
]
}
]
const mappedAvals = employees.map(emp => emp.aval.map(av => av.start));
console.log("************* MAPPED AVALS **************");
console.log(mappedAvals);
console.log("************* MAPPED AVALS **************");
const flattenedAvals = mappedAvals.reduce((acc, aval) => [...acc, ...aval], );
console.log("************* FLATTENED AVALS **************");
console.log(flattenedAvals);
console.log("************* FLATTENED AVALS **************");
const distinctAvals = [...(new Set(flattenedAvals))];
console.log("************* DISTINCT AVALS **************");
console.log(distinctAvals);
console.log("************* DISTINCT AVALS **************");
Thanks. Reduce is not my forte. may I ask a good source of learning ?
– brohymn
Nov 23 at 0:23
Trying out multiple examples like adding a list of numbers, counting the occurrences of characters, merging lists etc will definitely get you started but sorry, I am not aware of any particular good sources. The way you use the accumulator argument is the key to reduce method IMHO.
– Nandu Kalidindi
Nov 23 at 0:27
add a comment |
Assuming you wanted the aval list from employee list, you can try a combination of map
and reduce
Map to create arrays of array which will have all start aval values for a particular employee
employee
->aval
->start
Reduce to flatten the array of arrays
Finally use ES6
Set
to pick unique values.
const employees = [{
"EmployeeID": "56250f",
"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": "56250f",
"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"
}
]
}, {
"EmployeeID": "56250f",
"FirstName": "Charles",
"aval": [{
"start": "11-19",
"end": "2",
"ava": "100",
"health": "4"
},
{
"start": "11-20",
"end": "2",
"ava": "200",
"health": "4"
},
{
"start": "11-21",
"end": "2",
"ava": "300",
"health": "4"
},
{
"start": "11-22",
"end": "2",
"ava": "400",
"health": "4"
}
]
}
]
const mappedAvals = employees.map(emp => emp.aval.map(av => av.start));
console.log("************* MAPPED AVALS **************");
console.log(mappedAvals);
console.log("************* MAPPED AVALS **************");
const flattenedAvals = mappedAvals.reduce((acc, aval) => [...acc, ...aval], );
console.log("************* FLATTENED AVALS **************");
console.log(flattenedAvals);
console.log("************* FLATTENED AVALS **************");
const distinctAvals = [...(new Set(flattenedAvals))];
console.log("************* DISTINCT AVALS **************");
console.log(distinctAvals);
console.log("************* DISTINCT AVALS **************");
Assuming you wanted the aval list from employee list, you can try a combination of map
and reduce
Map to create arrays of array which will have all start aval values for a particular employee
employee
->aval
->start
Reduce to flatten the array of arrays
Finally use ES6
Set
to pick unique values.
const employees = [{
"EmployeeID": "56250f",
"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": "56250f",
"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"
}
]
}, {
"EmployeeID": "56250f",
"FirstName": "Charles",
"aval": [{
"start": "11-19",
"end": "2",
"ava": "100",
"health": "4"
},
{
"start": "11-20",
"end": "2",
"ava": "200",
"health": "4"
},
{
"start": "11-21",
"end": "2",
"ava": "300",
"health": "4"
},
{
"start": "11-22",
"end": "2",
"ava": "400",
"health": "4"
}
]
}
]
const mappedAvals = employees.map(emp => emp.aval.map(av => av.start));
console.log("************* MAPPED AVALS **************");
console.log(mappedAvals);
console.log("************* MAPPED AVALS **************");
const flattenedAvals = mappedAvals.reduce((acc, aval) => [...acc, ...aval], );
console.log("************* FLATTENED AVALS **************");
console.log(flattenedAvals);
console.log("************* FLATTENED AVALS **************");
const distinctAvals = [...(new Set(flattenedAvals))];
console.log("************* DISTINCT AVALS **************");
console.log(distinctAvals);
console.log("************* DISTINCT AVALS **************");
const employees = [{
"EmployeeID": "56250f",
"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": "56250f",
"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"
}
]
}, {
"EmployeeID": "56250f",
"FirstName": "Charles",
"aval": [{
"start": "11-19",
"end": "2",
"ava": "100",
"health": "4"
},
{
"start": "11-20",
"end": "2",
"ava": "200",
"health": "4"
},
{
"start": "11-21",
"end": "2",
"ava": "300",
"health": "4"
},
{
"start": "11-22",
"end": "2",
"ava": "400",
"health": "4"
}
]
}
]
const mappedAvals = employees.map(emp => emp.aval.map(av => av.start));
console.log("************* MAPPED AVALS **************");
console.log(mappedAvals);
console.log("************* MAPPED AVALS **************");
const flattenedAvals = mappedAvals.reduce((acc, aval) => [...acc, ...aval], );
console.log("************* FLATTENED AVALS **************");
console.log(flattenedAvals);
console.log("************* FLATTENED AVALS **************");
const distinctAvals = [...(new Set(flattenedAvals))];
console.log("************* DISTINCT AVALS **************");
console.log(distinctAvals);
console.log("************* DISTINCT AVALS **************");
const employees = [{
"EmployeeID": "56250f",
"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": "56250f",
"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"
}
]
}, {
"EmployeeID": "56250f",
"FirstName": "Charles",
"aval": [{
"start": "11-19",
"end": "2",
"ava": "100",
"health": "4"
},
{
"start": "11-20",
"end": "2",
"ava": "200",
"health": "4"
},
{
"start": "11-21",
"end": "2",
"ava": "300",
"health": "4"
},
{
"start": "11-22",
"end": "2",
"ava": "400",
"health": "4"
}
]
}
]
const mappedAvals = employees.map(emp => emp.aval.map(av => av.start));
console.log("************* MAPPED AVALS **************");
console.log(mappedAvals);
console.log("************* MAPPED AVALS **************");
const flattenedAvals = mappedAvals.reduce((acc, aval) => [...acc, ...aval], );
console.log("************* FLATTENED AVALS **************");
console.log(flattenedAvals);
console.log("************* FLATTENED AVALS **************");
const distinctAvals = [...(new Set(flattenedAvals))];
console.log("************* DISTINCT AVALS **************");
console.log(distinctAvals);
console.log("************* DISTINCT AVALS **************");
edited Nov 23 at 0:17
answered Nov 23 at 0:09
Nandu Kalidindi
3,7401921
3,7401921
Thanks. Reduce is not my forte. may I ask a good source of learning ?
– brohymn
Nov 23 at 0:23
Trying out multiple examples like adding a list of numbers, counting the occurrences of characters, merging lists etc will definitely get you started but sorry, I am not aware of any particular good sources. The way you use the accumulator argument is the key to reduce method IMHO.
– Nandu Kalidindi
Nov 23 at 0:27
add a comment |
Thanks. Reduce is not my forte. may I ask a good source of learning ?
– brohymn
Nov 23 at 0:23
Trying out multiple examples like adding a list of numbers, counting the occurrences of characters, merging lists etc will definitely get you started but sorry, I am not aware of any particular good sources. The way you use the accumulator argument is the key to reduce method IMHO.
– Nandu Kalidindi
Nov 23 at 0:27
Thanks. Reduce is not my forte. may I ask a good source of learning ?
– brohymn
Nov 23 at 0:23
Thanks. Reduce is not my forte. may I ask a good source of learning ?
– brohymn
Nov 23 at 0:23
Trying out multiple examples like adding a list of numbers, counting the occurrences of characters, merging lists etc will definitely get you started but sorry, I am not aware of any particular good sources. The way you use the accumulator argument is the key to reduce method IMHO.
– Nandu Kalidindi
Nov 23 at 0:27
Trying out multiple examples like adding a list of numbers, counting the occurrences of characters, merging lists etc will definitely get you started but sorry, I am not aware of any particular good sources. The way you use the accumulator argument is the key to reduce method IMHO.
– Nandu Kalidindi
Nov 23 at 0:27
add a comment |
You can also use flatMap
(if it's supported) and a Set
:
const aval = [...new Set(employees.flatMap(e => e.aval.map(a => a.start)))];
const employees = [{
"EmployeeID": "56250f",
"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": "56250f",
"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"
}
]
}, {
"EmployeeID": "56250f",
"FirstName": "Charles",
"aval": [{
"start": "11-19",
"end": "2",
"ava": "100",
"health": "4"
},
{
"start": "11-20",
"end": "2",
"ava": "200",
"health": "4"
},
{
"start": "11-21",
"end": "2",
"ava": "300",
"health": "4"
},
{
"start": "11-22",
"end": "2",
"ava": "400",
"health": "4"
}
]
}
]
const aval = [...new Set(employees.flatMap(e => e.aval.map(a => a.start)))];
console.log(aval);
1
This solution is much cleaner and it works , however there is a couple of things to consider. First, in Angular/ typescript, I have to change the data type of employees from any to any, which sometimes cannot be ideal. Also according to mdn IE and EDGE don't support it. they provide an alternative with reduce and concat tho.
– brohymn
Nov 23 at 15:41
@brohymn as for your second point about IE and Edge, the Typescript you write in your angular app is transpiled to Javascript that should work in all browsers: angular.io/guide/typescript-configuration
– slider
Nov 23 at 15:53
add a comment |
You can also use flatMap
(if it's supported) and a Set
:
const aval = [...new Set(employees.flatMap(e => e.aval.map(a => a.start)))];
const employees = [{
"EmployeeID": "56250f",
"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": "56250f",
"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"
}
]
}, {
"EmployeeID": "56250f",
"FirstName": "Charles",
"aval": [{
"start": "11-19",
"end": "2",
"ava": "100",
"health": "4"
},
{
"start": "11-20",
"end": "2",
"ava": "200",
"health": "4"
},
{
"start": "11-21",
"end": "2",
"ava": "300",
"health": "4"
},
{
"start": "11-22",
"end": "2",
"ava": "400",
"health": "4"
}
]
}
]
const aval = [...new Set(employees.flatMap(e => e.aval.map(a => a.start)))];
console.log(aval);
1
This solution is much cleaner and it works , however there is a couple of things to consider. First, in Angular/ typescript, I have to change the data type of employees from any to any, which sometimes cannot be ideal. Also according to mdn IE and EDGE don't support it. they provide an alternative with reduce and concat tho.
– brohymn
Nov 23 at 15:41
@brohymn as for your second point about IE and Edge, the Typescript you write in your angular app is transpiled to Javascript that should work in all browsers: angular.io/guide/typescript-configuration
– slider
Nov 23 at 15:53
add a comment |
You can also use flatMap
(if it's supported) and a Set
:
const aval = [...new Set(employees.flatMap(e => e.aval.map(a => a.start)))];
const employees = [{
"EmployeeID": "56250f",
"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": "56250f",
"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"
}
]
}, {
"EmployeeID": "56250f",
"FirstName": "Charles",
"aval": [{
"start": "11-19",
"end": "2",
"ava": "100",
"health": "4"
},
{
"start": "11-20",
"end": "2",
"ava": "200",
"health": "4"
},
{
"start": "11-21",
"end": "2",
"ava": "300",
"health": "4"
},
{
"start": "11-22",
"end": "2",
"ava": "400",
"health": "4"
}
]
}
]
const aval = [...new Set(employees.flatMap(e => e.aval.map(a => a.start)))];
console.log(aval);
You can also use flatMap
(if it's supported) and a Set
:
const aval = [...new Set(employees.flatMap(e => e.aval.map(a => a.start)))];
const employees = [{
"EmployeeID": "56250f",
"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": "56250f",
"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"
}
]
}, {
"EmployeeID": "56250f",
"FirstName": "Charles",
"aval": [{
"start": "11-19",
"end": "2",
"ava": "100",
"health": "4"
},
{
"start": "11-20",
"end": "2",
"ava": "200",
"health": "4"
},
{
"start": "11-21",
"end": "2",
"ava": "300",
"health": "4"
},
{
"start": "11-22",
"end": "2",
"ava": "400",
"health": "4"
}
]
}
]
const aval = [...new Set(employees.flatMap(e => e.aval.map(a => a.start)))];
console.log(aval);
const employees = [{
"EmployeeID": "56250f",
"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": "56250f",
"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"
}
]
}, {
"EmployeeID": "56250f",
"FirstName": "Charles",
"aval": [{
"start": "11-19",
"end": "2",
"ava": "100",
"health": "4"
},
{
"start": "11-20",
"end": "2",
"ava": "200",
"health": "4"
},
{
"start": "11-21",
"end": "2",
"ava": "300",
"health": "4"
},
{
"start": "11-22",
"end": "2",
"ava": "400",
"health": "4"
}
]
}
]
const aval = [...new Set(employees.flatMap(e => e.aval.map(a => a.start)))];
console.log(aval);
const employees = [{
"EmployeeID": "56250f",
"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": "56250f",
"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"
}
]
}, {
"EmployeeID": "56250f",
"FirstName": "Charles",
"aval": [{
"start": "11-19",
"end": "2",
"ava": "100",
"health": "4"
},
{
"start": "11-20",
"end": "2",
"ava": "200",
"health": "4"
},
{
"start": "11-21",
"end": "2",
"ava": "300",
"health": "4"
},
{
"start": "11-22",
"end": "2",
"ava": "400",
"health": "4"
}
]
}
]
const aval = [...new Set(employees.flatMap(e => e.aval.map(a => a.start)))];
console.log(aval);
answered Nov 23 at 0:33
slider
8,0251129
8,0251129
1
This solution is much cleaner and it works , however there is a couple of things to consider. First, in Angular/ typescript, I have to change the data type of employees from any to any, which sometimes cannot be ideal. Also according to mdn IE and EDGE don't support it. they provide an alternative with reduce and concat tho.
– brohymn
Nov 23 at 15:41
@brohymn as for your second point about IE and Edge, the Typescript you write in your angular app is transpiled to Javascript that should work in all browsers: angular.io/guide/typescript-configuration
– slider
Nov 23 at 15:53
add a comment |
1
This solution is much cleaner and it works , however there is a couple of things to consider. First, in Angular/ typescript, I have to change the data type of employees from any to any, which sometimes cannot be ideal. Also according to mdn IE and EDGE don't support it. they provide an alternative with reduce and concat tho.
– brohymn
Nov 23 at 15:41
@brohymn as for your second point about IE and Edge, the Typescript you write in your angular app is transpiled to Javascript that should work in all browsers: angular.io/guide/typescript-configuration
– slider
Nov 23 at 15:53
1
1
This solution is much cleaner and it works , however there is a couple of things to consider. First, in Angular/ typescript, I have to change the data type of employees from any to any, which sometimes cannot be ideal. Also according to mdn IE and EDGE don't support it. they provide an alternative with reduce and concat tho.
– brohymn
Nov 23 at 15:41
This solution is much cleaner and it works , however there is a couple of things to consider. First, in Angular/ typescript, I have to change the data type of employees from any to any, which sometimes cannot be ideal. Also according to mdn IE and EDGE don't support it. they provide an alternative with reduce and concat tho.
– brohymn
Nov 23 at 15:41
@brohymn as for your second point about IE and Edge, the Typescript you write in your angular app is transpiled to Javascript that should work in all browsers: angular.io/guide/typescript-configuration
– slider
Nov 23 at 15:53
@brohymn as for your second point about IE and Edge, the Typescript you write in your angular app is transpiled to Javascript that should work in all browsers: angular.io/guide/typescript-configuration
– slider
Nov 23 at 15:53
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53439224%2fget-nested-object-values-and-put-it-on-a-table-header%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
Can you upload the full data and format it?
– John Kennedy
Nov 23 at 0:03
Is your question how to create an array called
headers
like["11-19", "11-20", "11-21", "11-22"]
from theemployees.aval
array in the data you posted?– slider
Nov 23 at 0:08
data is inside the grid.component.ts the variable is called this.localObject , please open the stackblitz
– brohymn
Nov 23 at 0:10