Last active
February 20, 2022 19:23
-
-
Save husnuljahneer/024d5b14e1a3318bd5d4dfc4f0db9a94 to your computer and use it in GitHub Desktop.
JavaScript Snippets
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
// Push / Divide into multiple Arrays. | |
const product = [ | |
{ name: "iphone", price: 1200, availability: true }, | |
{ name: "samsung", price: 1100, availability: false }, | |
{ name: "huawei", price: 300, availability: true }, | |
{ name: "oppo", price: 400, availability: false }, | |
{ name: "vivo", price: 500, availability: true }, | |
{ name: "realme", price: 200, availability: false }, | |
{ name: "nokia", price: 1000, availability: true }, | |
{ name: "blackberry", price: 1000, availability: false }, | |
{ name: "lenovo", price: 900, availability: true }, | |
{ name: "xiaomi", price: 600, availability: false }, | |
{ name: "redmi", price: 500, availability: true }, | |
{ name: "blackberry", price: 800, availability: false }, | |
{ name: "lenovo", price: 500, availability: true }, | |
]; | |
const bifurcate = (arr, filter) => | |
arr.reduce( | |
(acc, val, i) => (acc[filter[i] ? 0 : 1].push(val), acc), [ | |
[], | |
[] | |
] | |
); | |
const result = bifurcate( | |
product.map((item) => { | |
return item.name.charAt(0).toUpperCase() + item.name.slice(1); | |
}), | |
product.map((item) => { | |
return item.availability; | |
}) | |
); | |
const available = result[0]; | |
const unavailable = result[1]; | |
console.log("AVAILBLE PRODUCTS : " + available); | |
console.log("UNAVAILABLE PRODUCTS : " + unavailable); | |
//AVAILBLE PRODUCTS : Iphone,Huawei,Vivo,Nokia,Lenovo,Redmi,Lenovo | |
//UNAVAILABLE PRODUCTS : Samsung,Oppo,Realme,Blackberry,Xiaomi,Blackberry |
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
// Assign to variables easly and quickly | |
let car = ['4.0L V8 Engine', 3982, 'Mercedes-Benz AMG GT'] | |
let [engine, cc, name] = car | |
console.log(name) // Mercedes-Benz AMG GT |
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
// Finding a specific object in an array of objects | |
let products = [ | |
{ id: 0, name: 'iphone' }, | |
{ id: 1, name: 'samsung' }, | |
{ id: 2, name: 'google' } | |
] | |
let customer = products.find(product => product.name === 'iphone') | |
console.log(products) | |
/* | |
0: {id: 0, name: 'iphone'} | |
1: {id: 1, name: 'samsung'} | |
2: {id: 2, name: 'google'} | |
/* |
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 product = [ | |
{ name: "iphone", price: 1200, availability: true }, | |
{ name: "samsung", price: 1100, availability: false }, | |
{ name: "huawei", price: 300, availability: true }, | |
{ name: "oppo", price: 400, availability: false }, | |
{ name: "vivo", price: 500, availability: true }, | |
{ name: "realme", price: 200, availability: false }, | |
{ name: "nokia", price: 1000, availability: true }, | |
{ name: "blackberry", price: 1000, availability: false }, | |
{ name: "lenovo", price: 900, availability: true }, | |
{ name: "xiaomi", price: 600, availability: false }, | |
{ name: "redmi", price: 500, availability: true }, | |
{ name: "blackberry", price: 800, availability: false }, | |
{ name: "lenovo", price: 500, availability: true }, | |
]; | |
const searchTerm = 'i'; | |
function productsSort(property) { | |
var sortOrder = 1; | |
if (property[0] === "-") { | |
sortOrder = -1; | |
property = property.substr(1); | |
} | |
return function(a, b) { | |
if (sortOrder == -1) { | |
return b[property].localeCompare(a[property]); | |
} else { | |
return a[property].localeCompare(b[property]); | |
} | |
} | |
} | |
product.sort(productsSort("name")); | |
const searchResult = products.filter(item => { | |
return item.name.includes(searchTerm); | |
}) | |
console.log(searchResult); | |
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
let first = [1,2,3] | |
let second = [3,4,5] | |
let combined = [..first, ...second]; | |
console.log(combined); // [1, 2, 3, 3, 4, 5] | |
console.log(...combined) // 1 2 3 4 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment