add sample code for object & JSON
parent
02744fb66f
commit
f84a501933
@ -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…
Reference in New Issue