Skip to content

Instantly share code, notes, and snippets.

@blackinitial
Last active May 21, 2020 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blackinitial/b7f4d2d9caf2a69d561188046669857f to your computer and use it in GitHub Desktop.
Save blackinitial/b7f4d2d9caf2a69d561188046669857f to your computer and use it in GitHub Desktop.
match array index selected with other array and sum
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