OneCompiler

complete javascript object

256

// jai Shri Ram*
// completer Object practice tutorial by *anurag verma

// I want to store information of student using array
var details = ["rahul", 25, "male", "banglore", "Coding"]
var marks = [12, 14, 45, 63]
var subject = ["Mathes", "Science","English", "hindi"];

// But using array it become very complicate
// difficulties for creating and organise
// ****************OBJECT *************

// storing a data using the form
// anyform which store data in daily life like google form , web form
// key should be unique

var user = {
Name: "Rahul",
age: 25,
gender: "Male",
City: "Banglore",
hobbies: ["coding", "football"], // you can add array
smoking: false,

};

// method of fetching an each element form user object

console.log(user);
// Using bracket method
console.log(user["Name"]);
console.log(user["hobbies"][1]);
console.log(user["smoking"]);
// using dot notation
user.city = "hydrabad";
user.hobbies[0]= "running";

user["mobile"]=8004833678;
user.collage = "XYZ";
user.hobbies.push("Eating");
user["hobbies"].push("kabbaddi");
user["hobbies"][1]= "bycycling";
user.hobbies[3]="cricket";

console.log(user);
delete user["city"];
delete user["gender"];
console.log(user);

var user10 = {
name: "Mahakal",
Dharm: "Hindu",
Bhagwan: ["Krishna", "Ram", "kalimata"]
};

console.log(user10);

// creating new object inside new object
var signup = {
name: "anurag",
lastName: "verma",
hobbies: [ 'coding', 'football','cricket' ],
password: 12345678,
address:{
state: "delhi",
pincode:110095,
},
age:{
month: "january",
year: "1998",
day: "05",
},
mobileNo: "492020570207"
}

console.log(signup.hobbies.length);
console.log(signup.age.month);
console.log(signup["address"]["state"]);
console.log(signup["address"]["pincode"]);

// accessing object using loop
// this for in loop is called special loop
// it is very easy to use

console.log((typeof(signup.name)));

for(key in signup){
console.log(key);
}

for(key in user){
console.log(user[key]);
}

for(key in user10){
console.log(user10[key]);
}

// use for in loop

var marks_of_student = {
Mathes:34,
HINDI:56,
SCINECE: 46,
HISTORY:78,

}
// use of for in loop

for(key in marks_of_student){
if (marks_of_student[key]<40) {
console.log(key);

}
}

// for(Object in user10){
// console.log(key);
// }

// Question:Given an array find the unique items in the array
// all unique items find this array

var arr = ["rajat", "rahul", "suraj", "Chirag","rajat", "rahul"]

var ispresent;
var unique = [];
for(let m=0; m<arr.length; m++){
ispresent = false;
for (var i = 0; i <unique.length; i++) {
if (arr[m]==arr[i]) {
ispresent=true;
}
}if(ispresent==false){
unique.push(arr[m]);
}
}console.log(unique);

var data = {
name: "Varun",
grade: 10,
collage:"xyz",
mark:{
math: 30,
science: 40,
english:60
},

}
console.log(data.mark);

// how i can access the data;
// you can put array inside array
//oject inside array
// array inside array

var user1 = {name:"Rajesh", age:30};

var users = [
{name:"Rajesh", age:33},
{name: "rahul", age:35},
{name: "Rudrah", age:45},
]

for(var i=0; i<users.length; i++){
console.log(users[i].age);
console.log(users[i].name);
}

var user1 = [
{name:"Rajesh", age:33},
{name: "rahul", age:45},
{name: "rudrah", age:55}
]
for(var i=0; i<user1.length; i++){
console.log(user1[i].age);
console.log(user1[i].name);
}
// find the sum of subjects marks

var store = {
name: "Rajat",
grade: "X",
marks: [
{ maths: 30, science: 40, english: 35 },
{ maths: 50, science: 90, english: 55 }
],

hobbies: ["coding", "running"]

};

for (var i = 0; i < store.marks.length; i++) {
var x= store.marks[i].maths+store.marks[i].science+store.marks[i].english
console.log(x);
}

// use of for in loop
for(let i=0; i<store.marks.length; i++){
var sum =0;
for(key in store.marks[i]){
console.log(store.marks[i][key]);
sum = sum +i;
}
}

// some other problem
var details = {
name:"shubham",
age:32,
print: function(){
console.log("Hello");

}
}

details.print();

// using other function
var introduction = {
name:"anurag verma",
age:24,
hobbies:["ghoomna", "khelna"],
express:function(){
console.log("Hiiii....",this.name,this.age,this.hobbies)
}
}

introduction.express();

// second same problem

var intro = {
name:"Shugandha",
age:23,
hobbies:["dancing", "eating"],
convey:function(){
console.log("Oyee...", this.name, "my age is ",this.age,
"my hobbies is ", this.hobbies[0])
}
}

intro.convey();
// adding to value in objects

var e_commerse = {
product:["earphone", "headphone","earpodes"],
quentity:[4,3,2],
price:[1300, 4500, 5230]
};

var total = 0;
for(var i=0;i<e_commerse.quentity.length;i++){
total = total+(e_commerse.quentity[i]+e_commerse.price[i]);
}console.log(total)

// adding some value inside the objects
var detail = {
data1: [],
addStudent: function(name, maths, science, english){
var obj = {};
obj["name"]= name;
obj["maths"]= maths;
obj["science"]= science;
obj["english"]= english;
this.data1.push(obj);
}
}

detail.addStudent("shubham", 30, 99, 95);
console.log(detail.data1);
detail.addStudent("pakal", 49, 89, 100);
console.log(detail.data1);

// question find the area and perimeter of tringle using function
var rectangle= {
length:30,
breadth: 40,
perimeter: function(){
console.log(2*(this.length + this.breadth));
},
area: function(){
console.log(this.length* this.breadth);
}
}

rectangle.perimeter();
rectangle.area();
// you can change you data
rectangle.length = 70;
rectangle.breadth = 100;
rectangle.perimeter();
rectangle.area();

// shop problem
// find total value using loop

var shop = {
product:["laptop", "mobile", "camara"],
quentity:[12, 8, 4],
price:[45000, 10000, 20000],
total_price : function(){
var count= 0;
for(let i=0; i<this.quentity.length;i++){
count = count + (this.quentity[i]* this.price[i]);
}
console.log("net value is ", count);

}
}

shop.total_price();