v2.5.2
Giriş yap

Javascript reduce() Metodu

ES5

reduce() metodu, dizinin her değeri için bir işlev yürütür ve sonunda diziyi tek bir değere düşürür.
Metodun dönüş değeri ilk parametresi olan accumulator parametresinde saklanır.

Yapısı (Syntax)array.reduce(function(accumulator, currentValue, index, arr), initialValue)

Parametreler

  • accumulator
    ( ! ) Notice: Undefined property: stdClass::$type in /home/prototurk.com/public_html/app/view/article-js.php on line 64
    Call Stack
    #TimeMemoryFunctionLocation
    10.0001360288{main}( ).../index.php:0
    20.0027444456require( '/home/prototurk.com/public_html/app/controller/category.php' ).../index.php:101
    30.0054456088require( '/home/prototurk.com/public_html/app/view/article.php' ).../category.php:34
    40.0116536080parseTemplate( ).../article.php:112
    50.0116536192preg_replace_callback ( ).../template.php:126
    60.0116536712bb_json( ).../template.php:126
    70.0118568016require( '/home/prototurk.com/public_html/app/view/article-js.php' ).../template.php:220
    Dönüş değeri
  • currentValue
    ( ! ) Notice: Undefined property: stdClass::$type in /home/prototurk.com/public_html/app/view/article-js.php on line 64
    Call Stack
    #TimeMemoryFunctionLocation
    10.0001360288{main}( ).../index.php:0
    20.0027444456require( '/home/prototurk.com/public_html/app/controller/category.php' ).../index.php:101
    30.0054456088require( '/home/prototurk.com/public_html/app/view/article.php' ).../category.php:34
    40.0116536080parseTemplate( ).../article.php:112
    50.0116536192preg_replace_callback ( ).../template.php:126
    60.0116536712bb_json( ).../template.php:126
    70.0118568016require( '/home/prototurk.com/public_html/app/view/article-js.php' ).../template.php:220
    İşlemde olan mevcut öğe
  • arr
    ( ! ) Notice: Undefined property: stdClass::$type in /home/prototurk.com/public_html/app/view/article-js.php on line 64
    Call Stack
    #TimeMemoryFunctionLocation
    10.0001360288{main}( ).../index.php:0
    20.0027444456require( '/home/prototurk.com/public_html/app/controller/category.php' ).../index.php:101
    30.0054456088require( '/home/prototurk.com/public_html/app/view/article.php' ).../category.php:34
    40.0116536080parseTemplate( ).../article.php:112
    50.0116536192preg_replace_callback ( ).../template.php:126
    60.0116536712bb_json( ).../template.php:126
    70.0118568016require( '/home/prototurk.com/public_html/app/view/article-js.php' ).../template.php:220
    İşlemden geçen elemanın ait olduğu dizi
  • initialValue
    ( ! ) Notice: Undefined property: stdClass::$type in /home/prototurk.com/public_html/app/view/article-js.php on line 64
    Call Stack
    #TimeMemoryFunctionLocation
    10.0001360288{main}( ).../index.php:0
    20.0027444456require( '/home/prototurk.com/public_html/app/controller/category.php' ).../index.php:101
    30.0054456088require( '/home/prototurk.com/public_html/app/view/article.php' ).../category.php:34
    40.0116536080parseTemplate( ).../article.php:112
    50.0116536192preg_replace_callback ( ).../template.php:126
    60.0116536712bb_json( ).../template.php:126
    70.0118568016require( '/home/prototurk.com/public_html/app/view/article-js.php' ).../template.php:220
    Başlangıç değeri (sayı, dizi, obje vb. olabilir)

Dönen Değer

Geriye biriken değer döner.

NOT
Eğer dizi boş ise bu metod çalışmaz.
Varolan diziyi etkilemez.

Örnekler

reduce() ile dizideki tüm değerlerin toplamını bulmak

let sum = [0, 1, 2, 3].reduce(function (accumulator, currentValue) {
  return accumulator + currentValue
}, 0)
console.log(sum) // 6

// alternatif olarak arrow function ile şöyle yazılabilir
let total = [ 0, 1, 2, 3 ].reduce(( accumulator, currentValue ) => accumulator + currentValue, 0)
console.log(total) // 6

Örneği Dene »

Nesne değerlerinin toplamını bulmak

const basket = [
    {
        name: "iPhone 8",
        price: 7000
    },
    {
        name: "Apple Macbook Pro",
        price: 14000
    },
    {
        name: "Harman/kardon",
        price: 255
    }
]
let sumBasket = basket.reduce((acc, item) => acc + item.price, 0)

console.log(sumBasket) // 21255

Örneği Dene »

Nesne değerlerinin tekrarını saymak

let names = ['Tayfun', 'Mehmet', 'Ahmet', 'Gökhan', 'Tayfun']

let countedNames = names.reduce(function (allNames, name) { 
  if (name in allNames) {
    allNames[name]++
  } else {
    allNames[name] = 1
  }
  return allNames
}, {})

console.log(countedNames)
// { Tayfun: 2, Mehmet: 1, Ahmet: 1, 'Gökhan': 1 }

Örneği Dene »

Değere göre nesneleri gruplama

const products = [
    {
        name: "NT1-A Condenser Mikrofon",
        brand: "Rode"
    },
    {
        name: "iPhone XS",
        brand: "Apple"
    },
    {
        name: "SmartLav Plus+ Yaka Mikrofonu",
        brand: "Rode"
    },
    {
        name: "M-AUDIO Keystation 61",
        brand: "M-Audio"
    }
]

const groupBy = (array, prop) => {
    return array.reduce((acc, obj) => {
        let key = obj[prop]
        if (!acc[key]){
            acc[key] = []
        }
        acc[key].push(obj)
        return acc
    }, {})
}

let groupedProducts = groupBy(products, 'brand')
console.log(groupedProducts)

Örneği Dene »

Spread operatörü ve initalValue değerini kullanarak nesne içindeki dizi değerlerini birleştirmek

const posts = [
  {
    title: "Post 1",
    tags: ["php", "css"],
  },
  {
    title: "Post 2",
    tags: ["js", "c#"],
  },
  {
    title: "Post 3",
    tags: ["html5", "css"],
  },
];

const allTags = posts.reduce((acc, post) => [...acc, ...post.tags], []);
console.log([...new Set(allTags)])

Örneği Dene »

Tekrarlanan değerleri silmek

Daha kolay kullanımı için Set()

let myArray = ['a', 'b', 'a', 'b', 'c', 'e', 'e', 'c', 'd', 'd', 'd', 'd']
let myOrderedArray = myArray.reduce(function (acc, currentValue) {
  if (!acc.includes(currentValue)) {
    acc.push(currentValue)
  }
  return acc
}, [])

console.log(myOrderedArray)

Örneği Dene »

filter() ve map() yerine reduce() kullanmak

const numbers = [-5, 6, 2, 0,]

console.log(
    numbers
    .filter(number => number > 0)
    .map(number => number * 2)
)

console.log(
    numbers.reduce((acc, number) => {
        if (number > 0){
            acc.push(number * 2)
        }
        return acc
    }, [])
)

Örneği Dene »

tayfunerbilen
1250 gün önce eklendi - 11142 kez görüntülendi.
Github'da Düzenle
Önceki forEach() Sonraki Python / Python Flask ve React Kullanımı