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);

  });
}

4
  • 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. Commented Aug 3, 2015 at 8: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 Commented Aug 3, 2015 at 8: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. Commented Aug 3, 2015 at 8: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":""}}]] Commented Aug 3, 2015 at 8:44

2 Answers 2

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");
Sign up to request clarification or add additional context in comments.

9 Comments

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
variable.propertyname will access a property in the object that variable contains.
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
yes i can get the property now thanks for your help :) @barmar
I've added an example of creating a button.
|
1

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

for(var i in array) {
    console.log(array[i]);
}

3 Comments

i have no experience with $.each so that if you can help me it would be of great help thanks
Use for(in). Is the same and you don't need jquery. If you need jquery api.jquery.com/jquery.each
for-in shouldn't be used with arrays: stackoverflow.com/questions/500504/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.