Reputation: 1042
I am currently looping through the data that I have, getting all values for a specific column and adding them to an array, then limiting it to only unique values.
I also have a reportSections
array that needs to be dynamic based on how many items are currently in my date_group
array. How can I loop through those arrays and add the values to the id
and name
objects within my reportSections
array?
Ideally, I'm thinking there might contain some sort of for loop that can loop through the reportSections
array and dynamically add the values inside the date_group
and record_group
arrays to the id
and name
objects, but not sure how that would work.
var date_group = [];
var record_date = [];
var i;
for (i = 0; i < data.length; i++) {
date_group.push(data[i]['date group']);
record_date.push(data[i]['record date']);
}
date_group = _.uniq(date_group);
record_date = _.uniq(record_date);
const reportSections = [
{id: 'date_group1', name: 'record_date1'},
{id: 'date_group2', name: 'record_date2'},
{id: 'date_group3', name: 'record_date3'},
{id: 'date_group4', name: 'record_date4'},
{id: 'date_group5', name: 'record_date5'},
{id: 'date_group6', name: 'record_date6'},
];
Upvotes: 0
Views: 89
Reputation: 3820
Regarding making items unique im confused looking at your question but try this solution.
Use set
for unique records
let data = [{
"date group": "C",
"record date": "13 Mar 20"
},
{
"date group": "B",
"record date": "11 Mar 20"
},
{
"date group": "A",
"record date": "11 Mar 20"
},
{
"date group": "B",
"record date": "12 Mar 20"
},
{
"date group": "C",
"record date": "11 Mar 20"
}
]
let reportSections = [];
let dataGroups = new Set();
let recordDates = new Set();
data.forEach(item => {
dataGroups.add(item['date group']);
recordDates.add(item['record date'])
})
for (const [index, value] of [...dataGroups].entries())
reportSections.push({
id: value,
name: [...recordDates][index]
})
console.log(reportSections);
.as-console-wrapper {
max-height: 100% !important;
top: 0;
}
Upvotes: 1