Skip to content

Instantly share code, notes, and snippets.

@ryandwi
Created January 16, 2020 02:46
Show Gist options
  • Save ryandwi/2fd61cf524d4a2355baa118e453b40c6 to your computer and use it in GitHub Desktop.
Save ryandwi/2fd61cf524d4a2355baa118e453b40c6 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/howeyaq
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
var products = [
{ name: 'cucumber', type: 'vegetable' },
{ name: 'banana', type: 'fruit' },
{ name: 'apple', type: 'fruit' },
{ name: 'orange', type: 'fruit' },
{ name: 'carrot', type: 'vegetable' },
];
// mengunakan for
var filteredProduct = []
for (let i = 0; i < products.length; i++) {
if (products[i].type === 'fruit') {
filteredProduct.push(products[i])
}
}
// mengunakan filter
let fruitProduct = products.filter((product) => product.type === 'fruit')
console.log(fruitProduct)
// console.log(filteredProduct)
</script>
<script id="jsbin-source-javascript" type="text/javascript">var products = [
{ name: 'cucumber', type: 'vegetable' },
{ name: 'banana', type: 'fruit' },
{ name: 'apple', type: 'fruit' },
{ name: 'orange', type: 'fruit' },
{ name: 'carrot', type: 'vegetable' },
];
// mengunakan for
var filteredProduct = []
for (let i = 0; i < products.length; i++) {
if (products[i].type === 'fruit') {
filteredProduct.push(products[i])
}
}
// mengunakan filter
let fruitProduct = products.filter((product) => product.type === 'fruit')
console.log(fruitProduct)
// console.log(filteredProduct)</script></body>
</html>
var products = [
{ name: 'cucumber', type: 'vegetable' },
{ name: 'banana', type: 'fruit' },
{ name: 'apple', type: 'fruit' },
{ name: 'orange', type: 'fruit' },
{ name: 'carrot', type: 'vegetable' },
];
// mengunakan for
var filteredProduct = []
for (let i = 0; i < products.length; i++) {
if (products[i].type === 'fruit') {
filteredProduct.push(products[i])
}
}
// mengunakan filter
let fruitProduct = products.filter((product) => product.type === 'fruit')
console.log(fruitProduct)
// console.log(filteredProduct)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment