0

I'm trying to get the value of fields object in the array o . I'm not able to get the value inside the property. 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);
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Wisely D Cruizer
  • 916
  • 9
  • 23

4 Answers4

3

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);
});
Martin Adámek
  • 16,771
  • 5
  • 45
  • 64
1

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=''>
Dan Philip Bejoy
  • 4,321
  • 1
  • 18
  • 33
1

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>
Community
  • 1
  • 1
Iceman
  • 6,035
  • 2
  • 23
  • 34
1

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.

anoopda
  • 74
  • 5