- Kategoriler
-
Başlıklar
- Videolu Anlatım
- Parametreler
- Dönen Değer
- Örnekler
reduce()ile dizideki tüm değerlerin toplamını bulmak- Nesne değerlerinin toplamını bulmak
- Nesne değerlerinin tekrarını saymak
- Değere göre nesneleri gruplama
- Spread operatörü ve initalValue değerini kullanarak nesne içindeki dizi değerlerini birleştirmek
- Tekrarlanan değerleri silmek
filter()vemap()yerinereduce()kullanmak
-
Paylaş
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 # Time Memory Function Location 1 0.0001 362360 {main}( ) .../index.php:0 2 0.0019 446424 require( '/home/prototurk.com/public_html/app/controller/category.php' ) .../index.php:101 3 0.0039 458056 require( '/home/prototurk.com/public_html/app/view/article.php' ) .../category.php:34 4 0.0092 538048 parseTemplate( ) .../article.php:112 5 0.0092 538160 preg_replace_callback ( ) .../template.php:126 6 0.0092 538680 bb_json( ) .../template.php:126 7 0.0093 569984 require( '/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 # Time Memory Function Location 1 0.0001 362360 {main}( ) .../index.php:0 2 0.0019 446424 require( '/home/prototurk.com/public_html/app/controller/category.php' ) .../index.php:101 3 0.0039 458056 require( '/home/prototurk.com/public_html/app/view/article.php' ) .../category.php:34 4 0.0092 538048 parseTemplate( ) .../article.php:112 5 0.0092 538160 preg_replace_callback ( ) .../template.php:126 6 0.0092 538680 bb_json( ) .../template.php:126 7 0.0093 569984 require( '/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 # Time Memory Function Location 1 0.0001 362360 {main}( ) .../index.php:0 2 0.0019 446424 require( '/home/prototurk.com/public_html/app/controller/category.php' ) .../index.php:101 3 0.0039 458056 require( '/home/prototurk.com/public_html/app/view/article.php' ) .../category.php:34 4 0.0092 538048 parseTemplate( ) .../article.php:112 5 0.0092 538160 preg_replace_callback ( ) .../template.php:126 6 0.0092 538680 bb_json( ) .../template.php:126 7 0.0093 569984 require( '/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 # Time Memory Function Location 1 0.0001 362360 {main}( ) .../index.php:0 2 0.0019 446424 require( '/home/prototurk.com/public_html/app/controller/category.php' ) .../index.php:101 3 0.0039 458056 require( '/home/prototurk.com/public_html/app/view/article.php' ) .../category.php:34 4 0.0092 538048 parseTemplate( ) .../article.php:112 5 0.0092 538160 preg_replace_callback ( ) .../template.php:126 6 0.0092 538680 bb_json( ) .../template.php:126 7 0.0093 569984 require( '/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.
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
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
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 }
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)
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)])
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)
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
}, [])
)