0

I was trying to do a reduce function like such to basically check if a thing meets a certain condition and make an element for the vdom for it.

I ended up not being able to do this because the reduce function complained about the function params did not match, it needed currentval, previousval, currentindex, and an array.

I was trying to follow the interface in JavaScript as seen with the MDN docs of accumulator, currentval...

  layerFields.reduce((acc, field) => {
    if (isValid(field)) {
      acc.push(
        <option value={field.name}>
          {field.alias}
        </option>
      );
    }
  });

I don't need previousValue, currentIndex, etc in my reduce, is there a way to do this? I guess could just implement it and just not do anything with those param values but then the linter would complain

0

1 Answer 1

1
layerFields.reduce((acc, field) => {
    if (isValid(field)) {
      acc.push(
        <option value={field.name}>
          {field.alias}
        </option>
      );
    }
    return acc;
  }, []);

You need the last param []. This is what acc is in the beginning.

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

Comments

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.