0

I need a function that returns an array of objects but currently, I'm getting an empty array as the return value.

I've got this code:

componentWillMount() {
    const data = {
        invoice: {
            documentID: '_e4564',
            displayName: '2019-02-03',
            url: 'https://www.urltoinvoice.com'
        },

        conditions: {
            documentID: '_e9365',
            displayName: 'Conditions company x',
            url: 'https://www.urltoconditions.com'
        },

        reminders: [
            {
                documentID: '_e4364',
                displayName: 'First reminder',
                url: 'https://www.urltofirstreminder.com'
            },
            {
                documentID: '_e0254',
                displayName: 'Second reminder',
                url: 'https://www.urltosecondreminder.com'
            },
        ]
    }

    this.setState({ 
        documents: this.getDocuments(data)
    })
}

getDocuments = documents => {
    const arr = [];

    function addDocument(documents, labelKey) {
        Object.entries(documents).forEach(([key, val]) => {
            if (Array.isArray(val)) {
                addDocument(val, key);
            } else {
                arr.push({ documentID: val.documentID, displayName: `${labelKey || key}: ${val.displayName}` });
            }
        });
    };

    return arr;
}

At this point, the code is not executing the addDocument function. Someone can tell me what I'm doing wrong?

Thore
  • 1,918
  • 2
  • 25
  • 50
  • 4
    If this is all the code, you never call `addDocument`. When are you wanting it to run? I don't know what initial arguments you want it to have, but just `addDocument(..., ...)` before the return would suffice. – Carcigenicate Mar 03 '19 at 23:52
  • 3
    Defining a function is not the same as executing it. If you want it executed you have to *call* it explicitly, which you're not doing. – Robin Zigmond Mar 03 '19 at 23:53
  • You are not calling the function 'addDocument' inside the main function. – Subhashi Mar 03 '19 at 23:56
  • Sorry. Added more code – Thore Mar 04 '19 at 00:01
  • @Carcigenicate I want the addDocument function to be called when getDocuments is called and when addDocument is called inside addDocument. Just looking for a way to return an array without creating an empty array variable in componentWillMount and pass it down – Thore Mar 04 '19 at 00:04

1 Answers1

1

Invocation of addDocument added before return statement:

getDocuments = documents => {
      const arr = [];

      function addDocument(documents, labelKey) {
        Object.entries(documents).forEach(([key, val]) => {
            if (Array.isArray(val)) {
                addDocument(val, key);
            } else {
                arr.push({ documentID: val.documentID, displayName: `${labelKey || key}: ${val.displayName}` });
            }
        });
      };

      addDocument(documents) ;

      return arr;
    }

addDocument is invoked with only documents parameter because of requirements in Keep original key when re-looping through nested object

chriss
  • 669
  • 4
  • 9
  • 1
    schematically this is indeed what the OP should do - but `labelKey is not defined here, and it's not at all obvious from this small snippet what the OP wants this second parameter to be when calling the function. – Robin Zigmond Mar 03 '19 at 23:56
  • copy paste error, should actually be empty it is used for if(Array – chriss Mar 03 '19 at 23:59
  • 1
    This answer could probably be improved by explaining some things. It's not clear at a glance how the code differs from that in the OP, or indeed how/why it answers the question. – Dave S Mar 03 '19 at 23:59
  • That's just an assumption that it "should be empty". I see that the function allows for it to be empty, but presumably the OP will sometimes want it over-ridden. Without knowing the OP's intent this is just guesswork. – Robin Zigmond Mar 04 '19 at 00:00
  • It isn't assumption because i answered it here https://stackoverflow.com/questions/54974417/keep-original-key-when-re-looping-through-nested-object/54974537#54974537 – chriss Mar 04 '19 at 00:01
  • @chriss - thanks for the link, I see now. The OP should have linked to that in the original question or included the full requirements there. Not all of us can be aware of every question! – Robin Zigmond Mar 04 '19 at 08:19
  • Yes sorry, my mistake not adding link immediately. I edited answer later – chriss Mar 04 '19 at 09:15