Reputation: 47
I have a javascript object as follows:
data = {
'id': my_id,
'report': my_report,
'type': my_selection,
'select': {
'valid': {
'table': {
'pass': ['x', 'y', 'z'],
'fail': ['x', 'y', 'z']
}
},
'invalid': {
'table': {
'pass': ['x', 'y', 'z'],
'fail': ['x', 'y', 'z']
}
}
}
};
I would like to use an iterator (double iterator?) to extract all the valid/invalid table pass/fail data.
So, I want to create an iterator that takes the arguments ('valid', 'invalid') and another with the arguments ('pass', 'fail').
I am using this snippet as an example for getting one of the iterators working:
function iterate() {
let items = [];
for (let iterator of arguments) {
items.push(data.select[iterator]);
}
return items;
}
var selector_types = iterate('valid', 'invalid');
This returns the 'table' objects as expected:
0 {table: Object}
1 {table: Object}
But ideally the iterate() would take two sets of args, something like:
function iterate() {
let items = [];
for (let iterator of arguments) {
items.push(data.select[iterator[0]].table[iterator[1]]);
}
return items;
}
var selector_types = iterate(['valid', 'invalid'], ['pass', 'fail']);
The idea of this is to get the pass/fail data for both the valid/invalid keys in one go. This (of course) doesn't work and returns undefined.
Is there an iterative solution to what I am attempting?
Regards,
MJ
Upvotes: 1
Views: 55
Reputation: 8752
How about some nested loops?
function iterate() {
let items = [];
for (let i of arguments[0]) {
for (let j of arguments[1]) {
items.push(data.select[i].table[j]);
}
}
return items;
}
You loop through the states valid
and invalid
, and for each one, you then loop through its pass
and fail
states, meaning you will loop through four states in total.
Upvotes: 1