Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

reduce

// Array.prototype.reduce()

const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;

// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10

// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15
Comment

reduce

var sum = _.reduce([1, 2, 3], function(memo, num){ return memo + num; }, 0);
=> 6
Comment

Reducer

// Arrow function
reduce((accumulator, currentValue) => { ... } )
reduce((accumulator, currentValue, index) => { ... } )
reduce((accumulator, currentValue, index, array) => { ... } )
reduce((accumulator, currentValue, index, array) => { ... }, initialValue)

// Callback function
reduce(callbackFn)
reduce(callbackFn, initialValue)

// Inline callback function
reduce(function callbackFn(accumulator, currentValue) { ... })
reduce(function callbackFn(accumulator, currentValue, index) { ... })
reduce(function callbackFn(accumulator, currentValue, index, array){ ... })
reduce(function callbackFn(accumulator, currentValue, index, array) { ... }, initialValue)
Comment

Reducer

import jwt_decode from "jwt-decode";
const initState = {
  loading: false,
  signUpErrors: [],
  loginErrors: [],
  token: "",
  user: "",
};

const verifyToken = (token) => {
  const decodeToken = jwt_decode(token);
  const expireIn = new Date(decodeToken.exp * 1000);
  if (new Date() > expireIn) {
    localStorage.removeItem("MyToken");
  } else {
    return decodeToken;
  }
};
const token = localStorage.getItem("MyToken");
if (token) {
  const decoded = verifyToken(token);
  initState.token = token;
  const { userData } = decoded;
  initState.user = userData;
}
const AuthReducer = (state = initState, action) => {
  if (action.type === "SET_LOADER") {
    return { ...state, loading: true };
  } else if (action.type === "CLOSE_LOADER") {
    return { ...state, loading: false };
  } else if (action.type === "SIGNUP_ERRORS") {
    return { ...state, signUpErrors: action.signupError };
  } else if (action.type === "SET_TOKEN") {
    const decoded = verifyToken(action.token);
    const { userData } = decoded;
    return {
      ...state,
      token: action.token,
      user: userData,
      signUpErrors: "",
      loginErrors: "",
    };
  } else if (action.type === "LOGOUT") {
    return { ...state, token: "", user: "" };
  } else if (action.type === "LOGIN_ERRORS") {
    return { ...state, loginErrors: action.loginErrors };
  } else {
    return state;
  }
};
export default AuthReducer;
Comment

reduce

const arr = [5, 7, 1, 8, 4];const sum = arr.reduce(function(accumulator, currentValue) {  return accumulator + currentValue;});// prints 25console.log(sum);
Comment

PREVIOUS NEXT
Code Example
Javascript :: loop array 
Javascript :: how to auto click webpage in angular 
Javascript :: show selected text in textarea && activeElement 
Javascript :: how to check in js if element is li or is button 
Javascript :: como usar un use state 
Javascript :: jqgrid set filter programmatically 
Javascript :: remove underscore and uppercase the letter 
Javascript :: react native typescript nodejs timeout 
Javascript :: how to push an object into an array in reducer 
Javascript :: how to put the value in the fom using javascript 
Javascript :: xamarin forms create components from json 
Javascript :: liquid indicators in react native 
Javascript :: nodejs hpp github 
Javascript :: replicate component did update hooks 
Javascript :: distructuring null check 
Javascript :: inline default export arrow in js 
Javascript :: blacklist word discord.js 
Javascript :: call a method of component from outside react 
Javascript :: how to redirect from login page to other page if user is already logged in in angular using jwt 
Javascript :: Spotify analytics intergration 
Javascript :: app.post isnt a function 
Javascript :: jquery ajax send data to wordpressajax_url not defined 
Javascript :: best way to store db config in node js 
Javascript :: fly: Javascript 
Javascript :: vue send event plus variable 
Javascript :: Contentful Migration - Transform Entires 
Javascript :: powershell json check if property exists 
Javascript :: scroll to list element javascript 
Javascript :: turn any function into promise 
Javascript :: javascript state array and object cheat sheet 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =