ਜਾਵਾਸਕਰਿਪਟ ਐਰੇ : ਫੰਕਸ਼ਨ ਨੂੰ ਘੱਟ ਸਮਝਣਾ

ਸੰਟੈਕਸ

arr.reduce(callbackFunction, [initialValue])

ਕੀ ਕਰ ਘਟਾਏ ਕਰਦਾ ਹੈ?

ਘਟਾਓ ਇੱਕ ਫੰਕਸ਼ਨ ਹੈ, ਜੋ ਕਿ ਇੱਕ ਐਰੇ ਦੁਆਰਾ ਇਹਨਆ ਹੈ.

ਓਪਰੇਸ਼ਨ ਨੂੰ ਇੱਕ ਕਾਲਬੈਕ ਫੰਕਸ਼ਨ ਦੁਆਰਾ ਐਰੇ 'ਤੇ ਕੀਤੀ ਜਾ ਸਕਦੀ ਹੈ.

ਐਰੇ ਉੱਪਰ ਕਾਰਵਾਈ ਦੇ ਅੰਤ 'ਤੇ, ਰਿਟਰਨ ਨੂੰ ਇੱਕ ਸਿੰਗਲ ਦਾ ਮੁੱਲ ਨੂੰ ਘੱਟ

ਘਟਾਓ ਵਰਤਿਆ ਜਾ ਸਕਦਾ ਹੈ, ਉਦਾਹਰਣ ਲਈ, ਇੱਕ ਸ਼ਾਪਿੰਗ ਕਾਰਟ ਦਾ ਕੁੱਲ ਨੂੰ

ਉਦਾਹਰਨ

  ਦਿਉ ਏਆਰਆਰ = [1,2,3,4,5];
  arr.reduce( (accumulator, currentValue, currentIndex, ਐਰੇ) => { 
      //ਕੁਝ ਓਪਰੇਸ਼ਨ;
      ਵਾਪਸ ਆਉਣ ਨੂੰ ਕੁਝ;
  });

 

ਕਾਲਬੈਕ ਫੰਕਸ਼ਨ

ਕਾਲਬੈਕ ਫੰਕਸ਼ਨ ਐਰੇ ਵਿੱਚ ਹਰ ਇੱਕ ਤੱਤ ਦੇ ਲਈ ਪਛਾਣੀ ਹੈ, ਵਿਚ ਪਾਸ 4 ਬਹਿਸ, ਜਿਸ ਵਿੱਚ ਘੱਟ ਕਰ ਫੰਕਸ਼ਨ ਦੁਆਰਾ ਪਾਸ ਕਰ ਰਹੇ ਹਨ.

ਇਹ 4 ਬਹਿਸ ਫਿਰ ਸਾਡੇ ਕਾਲਬੈਕ ਫੰਕਸ਼ਨ ਵਿੱਚ ਵਰਤਿਆ ਜਾ ਸਕਦਾ ਹੈ.

ਚਾਰ ਬਹਿਸ ਲੈਦਾ ਹੈ

  1. accumulator – ਪਹਿਲੀ ਲੂਪ ਜ ਸ਼ੁਰੂਆਤੀ ਮੁੱਲ ਤੇ ਐਰੇ ਦੇ ਪਹਿਲੇ ਤੱਤ ਜੇ ਸਪਲਾਈ. ਪਿਛਲੇ ਲੂਪ ਦੀ ਫਿਰ ਮੁੱਲ.
  2. currentValue – ਐਰੇ ਵਿੱਚ ਮੌਜੂਦਾ ਤੱਤ. ਐਰੇ ਭਾਵ ਵਿਚ ਦੂਜਾ ਤੱਤ ਸ਼ੁਰੂ. ਏਆਰਆਰ[1], ਜਦ ਸ਼ੁਰੂਆਤੀ ਮੁੱਲ ਦਲੀਲ ਪਾਸ ਨਾ ਕੀਤਾ ਗਿਆ ਹੈ. If the initial value argument is passed then current value starts from the 1st element in the array arr[0].
  3. currentIndex – The index of the current element.
  4. ਐਰੇ – The whole array.

Working Example

ਦਿਉ ਏਆਰਆਰ = [1,2,3,4,5];

arr.reduce(
  (accumulator, currentValue, currentIndex, ਐਰੇ) => {
    console.log(`accumulator (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): ${accumulator}`);
    console.log(`currentValue (Loops through the array, starting at arr[1]): ${currentValue}`);
    console.log(`currentIndex: ${currentIndex}`);
    console.log(`array: ${ਐਰੇ}`);
    console.log('---');
    return accumulator + currentValue;
  }
);

ਆਉਟਪੁੱਟ

accumulator (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): 1
currentValue (Loops through the array, starting at arr[1]): 2
currentIndex: 1
ਐਰੇ: 1,2,3,4,5
---
accumulator (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): 3
currentValue (Loops through the array, starting at arr[1]): 3
currentIndex: 2
ਐਰੇ: 1,2,3,4,5
---
accumulator (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): 6
currentValue (Loops through the array, starting at arr[1]): 4
currentIndex: 3
ਐਰੇ: 1,2,3,4,5
---
accumulator (On 1st loop, accumulator is arr[0], then it is the returned value of the previous loop): 10
currentValue (Loops through the array, starting at arr[1]): 5
currentIndex: 4
ਐਰੇ: 1,2,3,4,5
---

 

Providing the initial value

Specifying the second parameter (initialValue) in the reduce function will cause initialValue to be passed in as the accumulator argument on the first loop.

Note that the current value on the 1st loop now starts at the first element of the array

Working Example

  ਦਿਉ ਏਆਰਆਰ = [1,2,3,4,5];

  arr.reduce(
  (accumulator, currentValue, currentIndex, ਐਰੇ) => {
    console.log(`accumulator ( On the 1st loop, this is the initial value, then the returned value of the previous loop): ${accumulator}`);
    console.log(`currentValue (Loops through the array, starting at arr[0]): ${currentValue}`);
    console.log(`currentIndex: ${currentIndex}`);
    console.log(`array: ${ਐਰੇ}`);
    console.log('---');
    return accumulator + currentValue;
  }, 0
);

ਆਉਟਪੁੱਟ

accumulator ( On the 1st loop, this is the initial value, then the returned value of the previous loop): 0
currentValue (Loops through the array, starting at arr[0]): 1
currentIndex: 0
ਐਰੇ: 1,2,3,4,5
---
accumulator ( On the 1st loop, this is the initial value, then the returned value of the previous loop): 1
currentValue (Loops through the array, starting at arr[0]): 2
currentIndex: 1
ਐਰੇ: 1,2,3,4,5
---
accumulator ( On the 1st loop, this is the initial value, then the returned value of the previous loop): 3
currentValue (Loops through the array, starting at arr[0]): 3
currentIndex: 2
ਐਰੇ: 1,2,3,4,5
---
accumulator ( On the 1st loop, this is the initial value, then the returned value of the previous loop): 6
currentValue (Loops through the array, starting at arr[0]): 4
currentIndex: 3
ਐਰੇ: 1,2,3,4,5
---
accumulator ( On the 1st loop, this is the initial value, then the returned value of the previous loop): 10
currentValue (Loops through the array, starting at arr[0]): 5
currentIndex: 4
ਐਰੇ: 1,2,3,4,5
---