add sample code for object & JSON

main
yikth 5 years ago
parent 02744fb66f
commit f84a501933

@ -8,14 +8,30 @@ console.log(accDetails);
console.log("My account balance is: $" + accDetails[1]);
//add/delete at the end
console.log("pop :"); names.pop(); console.log(names);
console.log("push :"); names.push('James'); console.log(names);
console.log("pop :");
names.pop();
console.log(names);
console.log("push :");
names.push('James');
console.log(names);
//add at the end and delete from the front
console.log("shift :"); names.shift(); console.log(names);
console.log("unshift :"); names.unshift('Lim'); console.log(names);
console.log("shift :");
names.shift();
console.log(names);
console.log("unshift :");
names.unshift('Lim');
console.log(names);
//add/delete at middle
console.log("splice :");
console.log(names.splice(1, 1, ["Lim", "jenny"]));
console.log("splice name :", names);
console.log("splice name1 :", names[2]);

@ -23,3 +23,8 @@ let futureValueAF = (principle, rate, years, ci =2) => { //Arrow Function
}
console.log(futureValueAF(10000, 3, 5, 1).toFixed(2));
// formulate single line arrow function for single line return
let futureValueAF1 = (principle, rate, years, ci =2) => (principle * (1 + rate/100/ci)**(ci * years));
console.log(futureValueAF1(10000, 3, 5, 1).toFixed(2));

@ -0,0 +1,24 @@
var banks = ["DBS", "HSBC", "OCBC", "UOB"]; //Array
/*for (i=0; i<banks.length; i++) {
console.log(banks[i]);
}
*/
/*
let myFunction = function(item, index) { //iterate through the listof items
console.log(`${item} is at index ${index}` );
}
banks.forEach(myFunction); //forEach takes a CallBackFunction
*/
banks.forEach(function (item, index) { // LOOP
console.log(`${item} is at index ${index}`);
});
//using the last parameter
banks.forEach(function (item, index, arr) {
console.log("Using Array Index")
console.log(arr[index]);
});

@ -0,0 +1,14 @@
var account = {
acountNo:"ADB3256",
name:"James",
type:"savings",
balance: 500,
deposit: function(amt){
this.balance = this.balance + amt;
}
};
account.deposit(100);
console.log(account.balance);

@ -0,0 +1,23 @@
//Searchig in an array of objects
var expenses = [{
type :"Education",
amount : 2000,
}, {
type:"Eating Out",
amount : 600,
}, {
type: "Entertainment",
amount : 500,
}];
console.log(expenses[1]);
console.log(expenses[2].amount);
const fnSearch = expenses.findIndex(function(item, index){
console.log(item);
console.log(index);
//return item.type === "Eating Out";
});
//fnSearch = 3;
//console.log(fnSearch);

@ -0,0 +1,20 @@
//Searchig in an array of objects
var expenses = [{
type :"Education",
amount : 2000,
}, {
type:"Eating Out",
amount : 600,
}, {
type: "Entertainment",
amount : 500,
}];
const fnSearch = expenses.findIndex(function(item, index, exp){
if (item.type === "Eating Out") {
//exp[index].amount = 700;
item.amount = 700;
}
});
console.log(expenses);

@ -0,0 +1,24 @@
//Searchig in an array of objects
var bankbal = [{
bname :"DBS",
amount : 20000,
}, {
bname:"OCBC",
amount : 6000,
}, {
bname: "UOB",
amount : 5000,
}];
const bankbalJSONstring = JSON.stringify(bankbal[0]);
const backtoObj = JSON.parse(bankbalJSONstring);
console.log(bankbal[0]);
console.log(bankbalJSONstring);
console.log(backtoObj);
var a = ["DBS:10000", "HSBC:20000"];
const aJSON = JSON.stringify(a);
console.log(a);
console.log(aJSON);

@ -0,0 +1,21 @@
//objects
var account = {
acountNo:"ADB3256",
name:"James",
type:"savings",
balance: 500
};
console.log("Account Number:" + account.acountNo);
console.log("Account Balance:" + account.balance);
//object members can be object
myObj = {
"name":"John",
"age":30,
"cars": {
"car1":"Ford",
"car2":"BMW",
"car3":"Fiat"
}
}
console.log(myObj.cars.car1);
Loading…
Cancel
Save