0

I'm trying to get JSON data by field name like this data.name and it return the desired data, but I have 25 fields in the array and I want to make this dynamically, using data + "." + variable, when I alert it returns [Object object].name, so how I can make it executable?

I tried many ways but all failed, please help me doing this.

$.ajax({
  type: "Get",
  url: "/Home/Report_Data",
  datatype: "json",
  dataSrc: "",
  contentType: 'application/json; charset=utf-8',
  data: {
    'Arrnagement': Arrnagement
  },
  success: function(data) {
    var result = getElementsById("LD_name LD_Loan_Type LD_id LD_Full_Name_AR LD_GENDER LD_BIRTH_INCORP_DATE LD_PS_MOTHER_NAME LD_Street_AR LD_TEL_MOBILE LD_EMPLOY_STATUS_D LD_EMPLYRS_Name LD_MARITAL_STATUS LD_PS_PL_OF_BIR_AR LD_wifeName LD_Effective_Interest_Rate LD_Contract_amount LD_Repayment_Amount LD_Sector_name LD_NUM_REPAYMENTS LD_Loan_Maturity LD_Orig_Contract_Date LD_Loan_CCY LD_Arrangement LD_COLLATERAL_TYPE LD_Description LD_COLLATERAL_VALUE LD_COLLATERAL_Currency LD_GUARANTOR_ID LD_NATIONALITY LD_G_Full_Name_En LD_G_DATE_OF_BIRTH LD_G_PLACE_OF_BIRTH LD_G_MOTHER_NAME_EN LD_HOUSING_LOAN_AREA_CLASS LD_HOUSING_PROPERTY_NATURE LD_HOUSING_LOAN_PURPOSE LD_HOUSING_PROPERTY_AREA");
    var jid;
    for (var i = 0; i < result.length; i++) {
      jid = (result[i].id.substring(3));
      var resulting = data[0].jid;
      alert(resulting);
      if (result[i].innerHTML = data[0].jid != "undefined") {
        result[i].innerHTML = data[0].jid;
      } else {
        result[i].innerHTML = "";
      }
    }

//jid = name;
//data[0].name returns "Joun"
//data[0]+"."+jid returns [object object].name but i need it to return "Joun"
Rick
  • 4,030
  • 9
  • 24
  • 35
  • `for in` and `for of` might help you on this one. `For in` returning the keys and `for of` the values. Also note that you can access property of object as an array. `myObject.key` is mostly the same as `myObject["key"]` – Zyigh Jul 19 '18 at 08:31
  • Possible duplicate of [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – Andreas Jul 19 '18 at 08:40
  • Zyigh it didn't work for me bacause I cant make myObject[0,"key"] – Ibrahim Salameh Jul 19 '18 at 09:14
  • Andreas thx it helped me – Ibrahim Salameh Jul 19 '18 at 09:15

4 Answers4

0

Try giving data[0][jid], we can give a variable in brackets also inorder to get the data

Hope it works

Sowdhanya
  • 63
  • 4
0

This should work. I changed the dot notation while accessing the object property.

$.ajax({
            type: "Get",
            url: "/Home/Report_Data",
            datatype: "json",
            dataSrc: "",
            contentType: 'application/json; charset=utf-8',
            data: { 'Arrnagement': Arrnagement },
            success: function (data) {

                var result = getElementsById("LD_name LD_Loan_Type LD_id LD_Full_Name_AR LD_GENDER LD_BIRTH_INCORP_DATE LD_PS_MOTHER_NAME LD_Street_AR LD_TEL_MOBILE LD_EMPLOY_STATUS_D LD_EMPLYRS_Name LD_MARITAL_STATUS LD_PS_PL_OF_BIR_AR LD_wifeName LD_Effective_Interest_Rate LD_Contract_amount LD_Repayment_Amount LD_Sector_name LD_NUM_REPAYMENTS LD_Loan_Maturity LD_Orig_Contract_Date LD_Loan_CCY LD_Arrangement LD_COLLATERAL_TYPE LD_Description LD_COLLATERAL_VALUE LD_COLLATERAL_Currency LD_GUARANTOR_ID LD_NATIONALITY LD_G_Full_Name_En LD_G_DATE_OF_BIRTH LD_G_PLACE_OF_BIRTH LD_G_MOTHER_NAME_EN LD_HOUSING_LOAN_AREA_CLASS LD_HOUSING_PROPERTY_NATURE LD_HOUSING_LOAN_PURPOSE LD_HOUSING_PROPERTY_AREA");

                var responseData = data[0];
                var jid;
                for (var i = 0; i < result.length; i++) {
                    jid = (result[i].id.substring(3));
                    var resulting = responseData[jid];
                    alert(resulting);
                    if (responseData[jid]) {
                        result[i].innerHTML = responseData[jid];
                    }
                    else {
                        result[i].innerHTML = "";
                    }
                }
Anshul Bansal
  • 1,833
  • 1
  • 13
  • 12
0

If do foo.bar you are getting a property with the name 'bar' on object foo. If you have some variable const bar = "qux" and you want to access property on some object with the same name as bar value /"qux"/ you just need to use square brackets - foo [bar], which will be the same as calling foo.qux; So, in your case you just need to use data[0][jid] instead of data [0].jid, supposing jid contains a string that is also a key in data[0].

0

You can just do data[0][variableName] and this will return the data you want. for example. If data had a json string [{ "Name" : "Jane Doe"}] You could execute it like this.

var variableName = "Name";
console.log(data[0][variableName])

This would return "Jane Doe". if you have your field names in an array you can loop through them using $.each or a for loop. For example say your json string is [{"First_Name" : "Jane", "Last_Name" : "Doe", "Age" : 32}] you could get all the values from the json string doing this.

var FieldNames = ["First_Name" , "Last_Name", "Age"]
$.each(FieldNames, function(i,item) {
    console.log(data[0][item])
}

OR

var FieldNames = ["First_Name" , "Last_Name", "Age"]
for(var i = 0; i < FieldNames.length; i++) {
    console.log(data[0][FieldNames[i]])
}
Jordan
  • 33
  • 6