Wisely D Cruizer
Wisely D Cruizer

Reputation: 1139

Looping through an object in JQuery

I'm trying to get the value of fields object in the object o . I'm not able to get the value inside the property fields. What am I doing wrong?

var o = {
  "templatename": "sdgds",
  "fields": {
    "s1_req_1": 1,
    "s1_req_2": 1,
    "s1_req_3": 1,
    "s1_req_4": 1,
    "s1_req_5": 1,
    "s1_req_6": 1,
    "s1_req_7": 1,
    "s1_req_8": 1,
    "s1_req_9": 1,
    "s1_req_10": 1,
    "s1_req_11": 1,
    "s1_req_12": 1,
    "s1_req_13": 1,
    "s1_req_14": 1,
    "v1_dm_1": 1,
    "v1_dm_5": 1,
    "v1_dm_6": 1,
    "f1_fs_3": 1,
    "f1_fs_1": 1,
    "f1_fs_2": 1,
    "e3_eh_19": 1,
    "f1_fs_11": 1,
    "s3_sh_1": 1,
    "s3_sh_6": 1,
    "s3_sh_7": 1,
    "v1_dm_7": 1,
    "v1_dm_13": 1,
    "v1_dm_9": 1
  },
  "customerid": 'SMRTsspd'
};

$('#template').val(o.templatename);
$(o.fields).each(function(t) {
  $('input[value=' + t.name + ']').prop('checked', true);
});

Upvotes: -1

Views: 66

Answers (4)

anoopda
anoopda

Reputation: 74

I did not get you. From what i have understood.

$(o.fields) does not make any sense.

o.fields should be an array in order to use each. try changing the code to

"fields": [
    {"s1_req_1": 1}
    {"s1_req_2": 1},
    ......]

and as for 'name' there is no key as name.

o.fields.each(function(t){

});

Can you post the full code.

Upvotes: 1

Dan Philip Bejoy
Dan Philip Bejoy

Reputation: 4381

Use For-In You can use a For-In loop to enumerate through an object to get each key value pair.

for(var key in o.fields){
  if (o.fields.hasOwnProperty(key)) {
    $('input[value=' + key + ']').prop('checked', true);
  }
}

var o = {
  "templatename": "sdgds",
  "fields": {
    "s1_req_1": 1,
    "s1_req_2": 1,
    "s1_req_3": 1,
    "s1_req_4": 1,
    "s1_req_5": 1,
    "s1_req_6": 1,
    "s1_req_7": 1,
    "s1_req_8": 1,
    "s1_req_9": 1,
    "s1_req_10": 1,
    "s1_req_11": 1,
    "s1_req_12": 1,
    "s1_req_13": 1,
    "s1_req_14": 1,
    "v1_dm_1": 1,
    "v1_dm_5": 1,
    "v1_dm_6": 1,
    "f1_fs_3": 1,
    "f1_fs_1": 1,
    "f1_fs_2": 1,
    "e3_eh_19": 1,
    "f1_fs_11": 1,
    "s3_sh_1": 1,
    "s3_sh_6": 1,
    "s3_sh_7": 1,
    "v1_dm_7": 1,
    "v1_dm_13": 1,
    "v1_dm_9": 1
  },
  "customerid": 'SMRTsspd'
};

$('#template').val(o.templatename);
for (var key in o.fields) {
  if (o.fields.hasOwnProperty(key)) {
    $('input[value=' + key + ']').prop('checked', true);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type='checkbox' value='s1_req_1'>
<input type='checkbox' value='s1_req_2'>
<input type='checkbox' value='s1_req_3'>
<input type='checkbox' value=''>
<input type='checkbox' value=''>

Upvotes: 1

Martin Ad&#225;mek
Martin Ad&#225;mek

Reputation: 18369

You can loop through object without jquery:

Object.keys(o.fields).forEach(function (key) {
    $('input[value=' + key + ']').prop('checked', o.fields[key]);
});

As others have said, o.fields is an object, so to iterate through its values, you need to extract its keys first, via Object.keys(o.fields) method. Then you can iterate through those keys via simple Array.forEach(keys). Finally to use the value, you can just do o.fields[key].

Or with jquery, use it like this:

$.each(o.fields, function(key, value) {
    $('input[value=' + key + ']').prop('checked', value);
});

Upvotes: 3

Iceman
Iceman

Reputation: 6145

Since, the post tag is jquery ,I'm giving a jQuery specific solution specifically $.each.

For Vanilla JS solutions, look at SO Post: How do I loop through or enumerate a JavaScript object?

var o = {
  "templatename": "sdgds",
  "fields": {
    "s1_req_1": 1,
    "s1_req_2": 1,
    "s1_req_3": 1,
    "s1_req_4": 1,
    "s1_req_5": 1,
    "s1_req_6": 1,
    "s1_req_7": 1,
    "s1_req_8": 1,
    "s1_req_9": 1,
    "s1_req_10": 1,
    "s1_req_11": 1,
    "s1_req_12": 1,
    "s1_req_13": 1,
    "s1_req_14": 1,
    "v1_dm_1": 1,
    "v1_dm_5": 1,
    "v1_dm_6": 1,
    "f1_fs_3": 1,
    "f1_fs_1": 1,
    "f1_fs_2": 1,
    "e3_eh_19": 1,
    "f1_fs_11": 1,
    "s3_sh_1": 1,
    "s3_sh_6": 1,
    "s3_sh_7": 1,
    "v1_dm_7": 1,
    "v1_dm_13": 1,
    "v1_dm_9": 1
  },
  "customerid": 'SMRTsspd'
};

$('#template').val(o.templatename);
$.each(o.fields, function(key, value) {
  console.log(key + ":" + value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 1

Related Questions