freecodecamp: basic data structures
I note passed challenge code for help.
Use an Array to Store a Collection of Data
"let yourArray=[1,true,"3",4,5]; // change this line"
Access an Array's Contents Using Bracket Notation
"myArray[1]="bb";"
Add Items to an Array with push() and unshift()
" arr.unshift("I",2,"three");
arr.push(7,"VIII",9);"
Remove Items from an Array with pop() and shift()
"let popped=arr.pop(); // change this line
let shifted=arr.shift(); // change this line"
Remove Items Using splice()
" // change code below this line
arr.splice(2,2);
// change code above this line"
Add Items Using splice()
" // change code below this line
arr.splice(0,2,'DarkSalmon','BlanchedAlmond');
// change code above this line
"
Copy Array Items Using slice()
" // change code below this line
arr= arr.slice(2,4);
return arr;"
Copy an Array with the Spread Operator
" // change code below this line
newArr.push([...arr]);
// change code above this line"
Combine Arrays with the Spread Operator
"let sentence=['learning',...fragment,'is','fun']; // change this line"
Check For The Presence of an Element With indexOf()
" // change code below this line
if(arr.indexOf(elem)=== -1)
return false;
else
return true;
// change code above this line"
Iterate Through All an Array's Items Using For Loops
" // change code below this line
for(let i=0;i<arr.length;i++){
if(arr[i].indexOf(elem)== -1){
newArr.push(arr[i]);
}
}
// change code above this line"
Create complex multi-dimensional arrays
" // change code below this line
['unshift', false, 1, 2, 3, 'complex', 'nested'],
['loop', 'shift', 6, 7, 1000, 'method'],
['concat', false, true, 'spread', 'array'],
['mutate', 1327.98, 'splice', 'slice', 'push'],
[[[['deepest', 1.3849, 7], 'deeper'], 'deep'], 'depth']
// change code above this line"
Add Key-Value Pairs to JavaScript Objects
"// change code below this line
foods.bananas=13;
foods.grapes=35;
foods.strawberries=27;
// change code above this line"
Modify an Object Nested Within an Object
"userActivity.data.online=45;"
Access Property Names with Bracket Notation
"return foods[scannedItem];"
Use the delete Keyword to Remove Object Properties
" delete foods.oranges;
delete foods.plums;
delete foods.strawberries;"
Check if an Object has a Property
" if('Alan','Jeff','Sarah','Ryan' in obj)
return true;
else
return false;"
Iterate Through the Keys of an Object with a for...in Statement
" let count=0;
for(let u in obj){
if(obj[u].online)
count+=1;
;
}
return count;"
Generate an Array of All Object Keys with Object.keys()
"return Object.keys(obj);"
Modify an Array Stored in an Object
"userObj.data.friends.push(friend);
return userObj.data.friends;"
Nhận xét
Đăng nhận xét