JavaScript jquery each not working propertly while looping through jquery each not working,jquery each not working on array,

You javascript/jquery code has multiple issues. have a look at this

var populateList = function () {
    var accData = [{ A: "A1" }, { B: "B1"}];

    $.each(accData, function (index) {
        for(var value in accData[index]){
            alert(accData[index][value])
        }

    });

}

populateList();

I would rather suggest you to rectify the issues your self and ask in comment.

http://jsfiddle.net/fLEAw/3/

Answer:1

You can use $.each to loop arrays and objects:

$.each(accData, function(i, obj) {    
  $.each(obj, function(k, value) {
    alert(value);
  });
});

I doubt you'll end up using alert, you probably want the values to do something with them, so you can put them in an array. Here's an alternative re-usable approach in plain JavaScript:

var values = function(obj) {
  var result = [];
  for (var i in obj) {
    result.push(obj[i]);
  }
  return result;
};

var flatten = function(xs) {
  return Array.prototype.concat.apply([], xs);
};

var result = flatten(accData.map(values));

console.log(result); //=> ["A1", "B1"]
Answer:2

Change

populateList: function () {

to

function populateList() {

Write:

populateList();
function populateList() {
    var accData = [{
        A: "A1"
    }, {
        B: "B1"
    }];
    var len = accData.length;
    for (var i = 0; i < len; i++) {
        $.each(accData[i], function (key, value){
            //key will return key like A,B and value will return values assigned
            alert(value)
        });
    }
}

Updated fiddle here.

Answer:3

Since your array element is object, try this solution:

var populateList = function () {
  var accData = [{ A: "A1" }, { B: "B1"}];

  $.each(accData, function (index) {    
    for(var ele in accData[index]){
      alert(accData[index][ele]);
    }
  });

};
populateList();

Demo

Answer:4

I have two text fields with radio buttons. When I click on 1st button that text field should be active and other text field should be inactive. But, When I click on 2nd button both text fields are ...

I have two text fields with radio buttons. When I click on 1st button that text field should be active and other text field should be inactive. But, When I click on 2nd button both text fields are ...

How to filter data for a nested Kendo UI datasource in Hierarchy grid Below is the datasource: Obj1{"Name":"abc","id":1 ,Obj2 {{"Name":"A1","oid":1},{"Name":"A2","oid":2}} I need to search for Obj2 ...

How to filter data for a nested Kendo UI datasource in Hierarchy grid Below is the datasource: Obj1{"Name":"abc","id":1 ,Obj2 {{"Name":"A1","oid":1},{"Name":"A2","oid":2}} I need to search for Obj2 ...

  1. filter nested data
  2. filter nested data d3
  3. filter nested data r
  4. json filter nested data
  5. dplyr filter nested data frame
  6. js filter nested data
  7. javascript filter nested data

I am using Foundation 5 Accordions on a Website. They work but I want to change the transition speed. Currently when you click they instantly hide one and show the other. I would prefer they ...

I am using Foundation 5 Accordions on a Website. They work but I want to change the transition speed. Currently when you click they instantly hide one and show the other. I would prefer they ...

I am new to jquery, am working on small table based game here i have to find the neighboring elements around an TD in a table . EX: 1 2 3 4 5 6 7 8 9 there are three rows in a table , if i ...

I am new to jquery, am working on small table based game here i have to find the neighboring elements around an TD in a table . EX: 1 2 3 4 5 6 7 8 9 there are three rows in a table , if i ...