1

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

2
  • @MJ, could you please let me know , what your end result should be? Commented May 23, 2020 at 1:17
  • @Harmandeep Singh Kalsi, the data in question is in a localStorage var. I want to read that data in and extract all the relevant data from all the arrays, based on the selector (valid or invalid). Commented May 23, 2020 at 1:38

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the response @Toastrackenigma. I tried your solution and received the following: TypeError: undefined is not an object (evaluating 'data.select[i]')
@MikeJames Hmm, strange, it works for me. Are your data object and the arguments you are passing in the same as the ones in your question? Here's a JSfiddle of it running: jsfiddle.net/n0z9oa2c
basically yes. The data object I posted is structurally the same as the version of the data object I am using, but simplified by reducing the number of selectors and arrays, but is copied directly from my prototype that creates the object. I'll play around with it a bit and see what the issue is... Thx!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.