Check if object properties exist
hasOwnProperty ()
var myObj = {gift: "Pony", Pet: "Kitten", Bed: "Sleigh"}; function Checkobj (checkprop) {//Your Code here if (Myobj.hasownproperty (Checkprop)) return MYOBJ[CHECKP ROP]; Return "Not Found"; }
2. View JSON Object properties
var contacts = [ { " FirstName ": " Akira ", " LastName ": " Laine ", "number": "0543236543", "likes": ["Pizza", "Coding", "brownie points"] }, { "FirstName": "Harry", "LastName": "Potter", " Number ": " 0994372684 ", " likes ": [" Hogwarts ", " Magic ", " Hagrid "] }, { "FirstName": "Sherlock", "LastName": " Holmes ", &NBSP;&NBSP;&NBSP;&NBSP;&NBsp; "number": "0487345643", "likes": ["intriguing cases", "Violin"] }, { "FirstName": "Kristian", "LastName": "Vos", "number": "Unknown", "likes": ["Javascript", "Gaming", "Foxes"] }];function lookup (firstname, prop) {// only change code below this line for (var i =0; i< contacts.length;i++) { if (Firstname == contacts[i].firstname) { if (Contacts[i].hasownproperty (prop)) { return contacts[i][prop]; } else return ' no Such property '; } } return ' No such contact ';// only change code above This line}
3. Regular expressions
Example: If we want to find the The dog chased the cat word in the string the , we can use the following regular expression:/the/gi
We can divide this regular expression into several segments:
/It's the head of this regular expression.
theIs the pattern we want to match.
/is the tail of this regular expression.
g represents global (global) means that all matches are returned, not just the first one.
iIt means ignoring the case, meaning that when we look for a matching string, we ignore the case of the letter.
4. create an Object
var Car = function (wheels, seats, engines) {this.wheels = wheels; This.seats = seats; This.engines = engines;}; var myCar = new Car (6, 3, 1);
5. Map Traversal Array
The map method can be easily iterated into algebraic groups, examples: var timesfour = Oldarray.map (function (val) {return Val * 4;}); The map method iterates through each element in an algebraic group, processes each element according to the callback function, and finally returns a new array. Note that this method does not change the original array.
6.reduce
Array method Reduce is used to iterate over an array and accumulate it into a value. When using the reduce method, you pass in a callback function that has an accumulator (such as previousval in the example) and the current value (Currentval). The reduce method has an optional second parameter that can be used to set the initial value of the accumulator. If the initial value is not defined here, the initial value becomes the first item in the array, and Currentval begins with the second item in the array. var singleval = array.reduce (function (Previousval, currentval) {return previousval-currentval;}, 0);
7. Flipping a String
function ReverseString (str) {var Strre; var Wordre; Strre = Str.split (""); Strre.reverse (); for (var i =0; i< strre.length;i++) {wordre = Strre[i].split (""); Wordre.reverse (); Strre[i] = Wordre.join (""); } var result =strre.join (""); return result;} ReverseString ("Hello");
JavaScript with Notes