Work with Array , Objects and Array Objects in Jquery

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//Work with Array , Objects and Array Objects in Java Script
           //Work with Java Script Array
           var myArray = ['A1001', 'A1002', 'A1003', 'A1004', 'A1005'];
           //Findout value of element
           //myArray[4];                     //output - A1005
           //Findout index of element
           myArray.indexOf("A1002");       //output  - 1
           //Insert new item end of array
           myArray.push("A1006");       //now myArray is contains six elements ["A1001", "A1002", "A1003", "A1004", "A1005", "A1006"]
           //Update existing element's value
           myArray[4] = "A1010";       //now update array ["A1001", "A1002", "A1003", "A1004", "A1010"]
           //Findout length of array
           myArray.length = "A1010";       //Output -  5
           //Check type ($.isPlainObject function check that object is plain object or not)
           $.isPlainObject(myArray)      - Output - false
           //concat join two arrays and assign into new array
           myArray1 = ["A1006"];      
             
           var myArray3 = myArray.concat(myArray1);
           //now myArray3 is contains six elements ["A1001", "A1002", "A1003", "A1004", "A1005", "A1006"]
           //Work with Java Script Object Array
           var myObject1 = [{ 'A': 'A1001' }, { 'B': 'A1002' }, { 'C': 'A1003' }, { 'D': 'A1004' }, { 'E': 'A1005' }];
           //collection of objects
           var myObject2 = [{ 'F': 'A1006' }, { 'G': 'A1007' }, { 'H': 'A1008' }, { 'I': 'A1009' }, { 'J': 'A1010' }];
           
           //Check type ($.isPlainObject function check that object is plain object or not)
           $.isPlainObject(myObject1);
           //Output - false
           //Merges two objects, altering the first argument
           $.merge(myObject1, myObject2);
           //Now myObject1 is - [{"A":"A1001"},{"B":"A1002"},{"C":"A1003"},{"D":"A1004"},{"E":"A1005"},{"F":"A1006"},{"G":"A1007"},{"H":"A1008"},{"I":"A1009"},{"J":"A1010"}]
           //Merges two objects without any effect on both objects
           var myObject3 = $.merge($.merge([], myObject1), myObject2);
           //Now myObject3 is - [{"A":"A1001"},{"B":"A1002"},{"C":"A1003"},{"D":"A1004"},{"E":"A1005"},{"F":"A1006"},{"G":"A1007"},{"H":"A1008"},{"I":"A1009"},{"J":"A1010"}]
           
           var myNewObject4 = [{ 'K': 'A1011' }];
           //Inserting Object array
           myObject1.push(myNewObject4);
           //point of focus now object inserted as a Array at the end of collection
           //Now myObject1 is - [{ "A": "A1001" }, { "B": "A1002" }, { "C": "A1003" }, { "D": "A1004" }, { "E": "A1005" }, [{ "K": "A1011" }]]
           //Fetch object or value
            //myArray1[5][0] - Object {K: "A1011"}
            //myArray1[5][0].K - A1011
           var myNewObject5 = { 'K': 'A1011' };       //Inserting Object
           myObject1.push(myNewObject5);
           //Now myObject1 is - [{ "A": "A1001" }, { "B": "A1002" }, { "C": "A1003" }, { "D": "A1004" }, { "E": "A1005" }, { "K": "A1011" }]
            
           //Work on single or multiple object with muliple item
           var myObject3 = [[{ 'A': 'A1001', 'B': 'A1002', 'C': 'A1003', 'D': 'A1004', 'E': 'A1005' }]];     //single object
           var myObject4 = [[{ 'A': 'A1001', 'B': 'A1002', 'C': 'A1003', 'D': 'A1004', 'E': 'A1005' }, { 'F': 'A1006', 'G': 'A1007', 'H': 'A1008', 'I': 'A1009', 'J': 'A1010' }]];     //two object
           myObject3[0].push(myObject4[0][1]);        //now myObject3 looks like - [[{"A":"A1001","B":"A1002","C":"A1003","D":"A1004","E":"A1005"},{"F":"A1006","G":"A1007","H":"A1008","I":"A1009","J":"A1010"}]]
            
           //get value of A
           myObject3[0][0].A;  //output will be 'A1001'
//Work with Array , Objects and Array Objects in Java Script

           //Work with Java Script Array

           var myArray = ['A1001', 'A1002', 'A1003', 'A1004', 'A1005'];

           //Findout value of element
           //myArray[4];                     //output - A1005

           //Findout index of element
           myArray.indexOf("A1002");       //output  - 1

           //Insert new item end of array
           myArray.push("A1006");       //now myArray is contains six elements ["A1001", "A1002", "A1003", "A1004", "A1005", "A1006"]

           //Update existing element's value
           myArray[4] = "A1010";       //now update array ["A1001", "A1002", "A1003", "A1004", "A1010"]

           //Findout length of array
           myArray.length = "A1010";       //Output -  5

           //Check type ($.isPlainObject function check that object is plain object or not)
           $.isPlainObject(myArray)      - Output - false

           //concat join two arrays and assign into new array
           myArray1 = ["A1006"];       
            
           var myArray3 = myArray.concat(myArray1); 
           //now myArray3 is contains six elements ["A1001", "A1002", "A1003", "A1004", "A1005", "A1006"]

           //Work with Java Script Object Array

           var myObject1 = [{ 'A': 'A1001' }, { 'B': 'A1002' }, { 'C': 'A1003' }, { 'D': 'A1004' }, { 'E': 'A1005' }];

           //collection of objects

           var myObject2 = [{ 'F': 'A1006' }, { 'G': 'A1007' }, { 'H': 'A1008' }, { 'I': 'A1009' }, { 'J': 'A1010' }];

          
           //Check type ($.isPlainObject function check that object is plain object or not)

           $.isPlainObject(myObject1);

           //Output - false

           //Merges two objects, altering the first argument

           $.merge(myObject1, myObject2);

           //Now myObject1 is - [{"A":"A1001"},{"B":"A1002"},{"C":"A1003"},{"D":"A1004"},{"E":"A1005"},{"F":"A1006"},{"G":"A1007"},{"H":"A1008"},{"I":"A1009"},{"J":"A1010"}]

           //Merges two objects without any effect on both objects

           var myObject3 = $.merge($.merge([], myObject1), myObject2);
           //Now myObject3 is - [{"A":"A1001"},{"B":"A1002"},{"C":"A1003"},{"D":"A1004"},{"E":"A1005"},{"F":"A1006"},{"G":"A1007"},{"H":"A1008"},{"I":"A1009"},{"J":"A1010"}]
          
           var myNewObject4 = [{ 'K': 'A1011' }];
           //Inserting Object array
           myObject1.push(myNewObject4);

           //point of focus now object inserted as a Array at the end of collection
           //Now myObject1 is - [{ "A": "A1001" }, { "B": "A1002" }, { "C": "A1003" }, { "D": "A1004" }, { "E": "A1005" }, [{ "K": "A1011" }]]

           //Fetch object or value
            //myArray1[5][0] - Object {K: "A1011"}
            //myArray1[5][0].K - A1011


           var myNewObject5 = { 'K': 'A1011' };       //Inserting Object
           myObject1.push(myNewObject5);

           //Now myObject1 is - [{ "A": "A1001" }, { "B": "A1002" }, { "C": "A1003" }, { "D": "A1004" }, { "E": "A1005" }, { "K": "A1011" }]
           

           //Work on single or multiple object with muliple item

           var myObject3 = [[{ 'A': 'A1001', 'B': 'A1002', 'C': 'A1003', 'D': 'A1004', 'E': 'A1005' }]];     //single object

           var myObject4 = [[{ 'A': 'A1001', 'B': 'A1002', 'C': 'A1003', 'D': 'A1004', 'E': 'A1005' }, { 'F': 'A1006', 'G': 'A1007', 'H': 'A1008', 'I': 'A1009', 'J': 'A1010' }]];     //two object

           myObject3[0].push(myObject4[0][1]);        //now myObject3 looks like - [[{"A":"A1001","B":"A1002","C":"A1003","D":"A1004","E":"A1005"},{"F":"A1006","G":"A1007","H":"A1008","I":"A1009","J":"A1010"}]]
           

           //get value of A
           myObject3[0][0].A;  //output will be 'A1001'

Comments