Last active
May 21, 2020 02:27
-
-
Save blackinitial/b7f4d2d9caf2a69d561188046669857f to your computer and use it in GitHub Desktop.
match array index selected with other array and sum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const selected = [0, 2] | |
const products = [ | |
{ | |
price: 20000, | |
... | |
}, | |
... | |
] | |
totalPrice() { | |
let total = 0 | |
// get index from itearation (ES6) | |
for (const [productIndex, valueProduct] of products.entries()) { | |
// filter | |
selected.forEach((selectIndex) => { | |
if (selectIndex === productIndex) { | |
total += valueProduct.price | |
} | |
}) | |
} | |
return total | |
} | |
totalPrice() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment