2

The array is somewhat like this :

[[Object { button={...}}, 
  Object { input={...}}, 
  Object { checkbox={...}}, 
  Object { textarea={...}}],
 [Object { textarea={...}}]
]

In the curly brackets i have set some properties like color,value,type etc.
What i want is to get the each object of an array and check through the properties like type of an object and then call a function to perform further things. Just like in PHP we use :

foreach($a as $b){
 // and then do something here ..
}; 

Kindly help me through and i hope everyone can understand what i am trying to say.

// var i is the counter for page numbers   
function pagination(i) {
  alert(i);
  i--;
  //page is array 

  var result = page;
  //console.log(result[i]);
  var $currentElem;
  $(result[i]).each(function() {

    currentElem = $(this);
    console.log(currentElem);

  });
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
Abdul Basit
  • 125
  • 1
  • 3
  • 14
  • How does your `page` array look like? Can you show us an example? And what are you trying to do to the array? It doesn't come across as obvious in your question. – Terry Aug 03 '15 at 08:32
  • page array contain objects with their properties(as shown at the start) what i want from it to read array in jquery and access the objects in it – Abdul Basit Aug 03 '15 at 08:35
  • I mean, an **example** of your data, i.e. dummy array. That is also why we emphasise on posting minimal, verifiable and concrete examples, instead of pseudo-code that does not work. – Terry Aug 03 '15 at 08:37
  • its actually encoded in JSON [[{"button":{"type":"","color":"white","width":"15px","backgroundcolor":"white","value":" My Button","control":"button","name":"","label":"My Button","status":"","style":"","left":"","center":"","right":""}},{"input":{"type":"","color":"white","width":"15px","label":"Enter your text...","control":"textinput","name":"","status":"","style":"","left":"","center":"","right":""}}],[{"checkbox":{"type":"","fontcolor":"white","label":"My Checkbox","control":"checkbox","name":"","status":"","style":"","left":"","center":"","right":""}}]] – Abdul Basit Aug 03 '15 at 08:44

2 Answers2

4

.each is used when you're looping over the elements of a jQuery collection. To loop over the contents of an array or object. use $.each():

$.each(result[i], function(n, currentElem) {
    console.log(currentElem);
});

And you shouldn't use $(this) unless this is a DOM element. If it's just a Javascript object, wrapping it in a jQuery object is unnecessary.

You can access the properties using the normal Javascript variable.propertyname syntax, e.g. currentElem.button and currentElem.button.color. To append elements to your view, you can do something like:

var button = currentElem.button;
$("<button>", {
    value: button.value,
    name: button.name,
    css: {
        color: button.color,
        width: button.width,
        backgroundColor: button.backgroundcolor
    }
}).appendTo($("#buttonDiv");
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • yes barmar thanks but the next part is am having problem yes i get the objects of an array but i want to access these object and its properties can you help me with that @barmar – Abdul Basit Aug 03 '15 at 09:07
  • `variable.propertyname` will access a property in the object that `variable` contains. – Barmar Aug 03 '15 at 09:08
  • i want to get the object for example if its a button i would call a function that would append button to my view . i hope iam making sense and you can get what i want @barmar – Abdul Basit Aug 03 '15 at 09:09
  • yes i can get the property now thanks for your help :) @barmar – Abdul Basit Aug 03 '15 at 09:14
  • I've added an example of creating a button. – Barmar Aug 03 '15 at 09:14
  • $.each(result[i], function(n, currentElem) { console.log(currentElem); console.log(currentElem.button.color); if(currentElem == "button") { alert("accomplished"); } else { alert("not again "); } – Abdul Basit Aug 03 '15 at 09:20
  • yes i can access the properties but i want a scenario like above to check if the object is button then append button else do something.. @barmar – Abdul Basit Aug 03 '15 at 09:21
  • You can use `if(currentElem.button)` to tell if it's a button. – Barmar Aug 03 '15 at 09:27
  • yes thanks alot @barmar :) you made my day better thank you – Abdul Basit Aug 03 '15 at 09:35
1

To iterate you can use for(in) or in jquery $.each.

for(var i in array) {
    console.log(array[i]);
}
Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69