Created
May 14, 2020 03:55
-
-
Save sangkukbae12/529ca7be9be5806df66280425154cdda to your computer and use it in GitHub Desktop.
understand javascript arrays
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
// 1. Use Concat to Add Values to an Array | |
// Array.prototype.concat(); | |
var people = [ | |
{ | |
name: 'Shane' | |
}, | |
{ | |
name: 'Sally' | |
} | |
]; | |
var people2 = [ | |
{ | |
name: 'Simon' | |
}, | |
{ | |
name: 'Ben' | |
} | |
]; | |
people2.concat(people2) | |
.forEach(function (person) { | |
console.log(person.name) || displayInPreview(person.name); | |
}); | |
// display in plunker preview | |
function displayInPreview(string) { | |
var newDiv = document.createElement("div"); | |
var newContent = document.createTextNode(string); | |
newDiv.appendChild(newContent); | |
document.body.appendChild(newDiv) | |
} | |
// 2. Combine Values of an Array into a String with Join | |
var name = 'shane osbourne'; | |
var upper = name.split(' ') // [shane, osbourne] | |
.map(x => x.charAt(0).toUpperCase() + x.slice(1)) // [Shane, Osbourne] | |
.join(' '); | |
console.log(upper) || displayInPreview(upper); | |
// display in plunker preview | |
function displayInPreview(string) { | |
var newDiv = document.createElement("div"); | |
var newContent = document.createTextNode(string); | |
newDiv.appendChild(newContent); | |
document.body.appendChild(newDiv) | |
} | |
// 3. Check if a Value is in an Array with indexOf | |
// Array.prototype.indexOf(); | |
// see console for output! | |
var whitelist = ['.css', '.js']; | |
var events = [ | |
{ | |
file: 'css/core.css' | |
}, | |
{ | |
file: 'js/app.js' | |
}, | |
{ | |
file: 'index.html' | |
} | |
]; | |
var filtered = events.filter(event => { | |
var ext = event.file.substr(event.file.lastIndexOf('.')); | |
return whitelist.indexOf(ext) > -1; | |
}); | |
console.log(filtered) || displayInPreview(filtered[0].file) || displayInPreview(filtered[1].file); | |
// display in plunker preview | |
function displayInPreview(string) { | |
var newDiv = document.createElement("div"); | |
var newContent = document.createTextNode(string); | |
newDiv.appendChild(newContent); | |
document.body.appendChild(newDiv) | |
} | |
// 4. Create a Shallow Copy of an Array with Slice | |
// Array.prototype.slice(); | |
var person = { | |
name: 'shane-osbourne' | |
}; | |
var filters = { | |
'deslugify': x => x.replace('-', ' '), | |
'uppercase': x => x.toUpperCase() | |
}; | |
var input = 'name | deslugify | uppercase'; // SHANE OSBOURNE | |
var sections = input.split('|').map(x => x.trim()); // [name, deslugify, uppercase] | |
var ref = person[sections[0]]; | |
var output = sections | |
.slice(1) | |
.reduce((prev, next) => { | |
if (filters[next]) { | |
return filters[next].call(null, prev); | |
} | |
return prev; | |
}, ref); | |
console.log(output) || displayInPreview(output); | |
// display in plunker preview | |
function displayInPreview(string) { | |
var newDiv = document.createElement("div"); | |
var newContent = document.createTextNode(string); | |
newDiv.appendChild(newContent); | |
document.body.appendChild(newDiv) | |
} | |
// 5. Sort an Array Alphabetically or Numerically | |
// Array.prototype.sort(); | |
var lessons = [ | |
{ | |
title: 'Javascript Array methods in depth - concat', | |
views: 1000 | |
}, | |
{ | |
title: 'Javascript Array methods in depth - slice', | |
views: 1050 | |
}, | |
{ | |
title: 'Javascript Array methods in depth - join', | |
views: 1025 | |
} | |
]; | |
var list = lessons | |
.sort((a, b) => b.views - a.views) | |
.map(x => ` <li>${x.title} (${x.views})</li>`) | |
.join('\n'); | |
var output = `<ul>\n${list}\n</ul>`; | |
var container = document.querySelector('#output'); | |
container.innerHTML = output | |
// 6. Filter an Array with Truthy Values | |
// see console for output! | |
const lessons = [ | |
{ | |
title: 'Javascript Arrays in Depth - join', | |
views: 960, | |
tags: ['array', 'join'] | |
}, | |
{ | |
title: 'Javascript Arrays in Depth - concat', | |
views: 1050, | |
tags: ['array', 'concat'] | |
}, | |
{ | |
title: 'Javascript Arrays in Depth - slice', | |
views: 2503, | |
tags: ['array', 'slice'] | |
}, | |
{ | |
title: 'Javascript Functions in Depth - bind', | |
views: 2500, | |
tags: ['functions', 'bind'] | |
} | |
]; | |
const minViews = 1000; | |
const searchTerm = 'array'; | |
const filtered = lessons | |
.filter(x => x.tags.indexOf(searchTerm) > -1) | |
.filter(x => x.views > minViews) | |
.sort((a, b) => b.views - a.views) | |
.map(x => ` <li>${x.title}</li>`) | |
.join('\n'); | |
console.log(`<ul> | |
${filtered} | |
</ul>`) || displayInPreview(`<ul> | |
${filtered} | |
</ul>`); | |
// display in plunker preview | |
function displayInPreview(string) { | |
var newDiv = document.createElement("div"); | |
var newContent = document.createTextNode(string); | |
newDiv.appendChild(newContent); | |
document.body.appendChild(newDiv) | |
} | |
// 7. Use Some as a Ternary Operator or Conditional | |
// Array.prototype.some() | |
var tasks = [ | |
{ | |
title: 'Do laundry', | |
completed: true | |
}, | |
{ | |
title: 'Feed the cat', | |
completed: true | |
}, | |
{ | |
title: 'Watch the array lessons on egghead.io', | |
completed: true | |
} | |
]; | |
var list = document.querySelector('.task-list'); | |
list.classList.add( | |
tasks.some(task => task.completed === false) | |
? 'task-list--uncompleted' | |
: 'task-list--completed' | |
); | |
list.innerHTML = tasks | |
.map(x => x.completed ? `<s>${x.title}</s>` : x.title) | |
.map(x => `<li>${x}</li>`) | |
.join(''); | |
// 8. Add Elements onto an Array with Push | |
const input = document.querySelector('#input'); | |
const button = document.querySelector('#button'); | |
const list = document.querySelector('#list'); | |
const pets = []; | |
button.addEventListener("click", function (evt) { | |
if (input.value.length > 0) { | |
pets.push(input.value); | |
input.value = ""; | |
render(); | |
} | |
}); | |
function render () { | |
list.innerHTML = pets.map(x => `<li>${x}</li>`).join(''); | |
} | |
// 9. Modify Values in an Array with Map | |
// see console for output! | |
const items = [ | |
{ | |
active: true, | |
firstname: 'Shane', | |
lastname: 'Osbourne' | |
}, | |
{ | |
active: true, | |
firstname: 'Sally', | |
lastname: 'Osbourne' | |
}, | |
{ | |
active: false, | |
firstname: 'Ben', | |
lastname: 'Barker' | |
} | |
]; | |
const mapped = items | |
.filter(x => x.active) | |
.map(x => x.firstname); | |
function createHtmlList(items) { | |
const listElements = items.map(x => ` <li>${x}</li>`).join(''); | |
return `<ul>${listElements}</ul>`; | |
} | |
console.log(createHtmlList(mapped)) || displayInPreview(createHtmlList(mapped)) | |
// display in plunker preview | |
function displayInPreview(string) { | |
var newDiv = document.createElement("div"); | |
var newContent = document.createTextNode(string); | |
newDiv.appendChild(newContent); | |
document.body.appendChild(newDiv) | |
} | |
// 10. Validate Data with the Every() Method | |
// Array.prototype.every(); | |
var videos = [ | |
{ | |
title: 'Array methods in depth: concat', | |
length: 310, | |
viewed: 310 | |
}, | |
{ | |
title: 'Array methods in depth: join', | |
length: 420, | |
viewed: 360 | |
} | |
]; | |
function complete(x) { | |
return x.viewed === x.length; | |
} | |
var isComplete = videos.every(complete); | |
var completed = videos.filter(complete); | |
console.log(isComplete) | |
|| displayInPreview(isComplete); | |
console.log(completed) | |
|| displayInPreview(`title: ${completed[0].title}`) | |
|| displayInPreview(`length: ${completed[0].length}`) | |
|| displayInPreview(`viewed: ${completed[0].viewed}`); | |
// display in plunker preview | |
function displayInPreview(string) { | |
var newDiv = document.createElement("div"); | |
var newContent = document.createTextNode(string); | |
newDiv.appendChild(newContent); | |
document.body.appendChild(newDiv) | |
} | |
// 11. Produce a single value from an Array with reduce | |
// Array.prototype.reduce | |
const users = [ | |
{ | |
id: '01', | |
name: 'Shane' | |
}, | |
{ | |
id: '02', | |
name: 'Sally' | |
} | |
]; | |
const newUsers = users.reduce((obj, user) => { | |
obj[user.id] = user; | |
return obj; | |
}, {}); | |
console.log(newUsers['02'].name) || displayInPreview(newUsers['02'].name); | |
// display in plunker preview | |
function displayInPreview(string) { | |
var newDiv = document.createElement("div"); | |
var newContent = document.createTextNode(string); | |
newDiv.appendChild(newContent); | |
document.body.appendChild(newDiv) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment