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?

asked Mar 3, 2019 at 23:50
5
  • 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. Commented Mar 3, 2019 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. Commented Mar 3, 2019 at 23:53
  • You are not calling the function 'addDocument' inside the main function. Commented Mar 3, 2019 at 23:56
  • Sorry. Added more code Commented Mar 4, 2019 at 0: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 Commented Mar 4, 2019 at 0:04

1 Answer 1

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

answered Mar 3, 2019 at 23:55
Sign up to request clarification or add additional context in comments.

7 Comments

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.
copy paste error, should actually be empty it is used for if(Array
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.
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.
It isn't assumption because i answered it here stackoverflow.com/questions/54974417/…
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.