Search code examples
jsondata-bindingodatasapui5

How to get property from JSON model filled with oData?


Im trying to get property from oData return model. I set data in success callback function from oData to JSON model.

  oODataModel.read("/ConnObjSet?$filter=Objecttype eq 'CONNOBJ' and ConnObject eq '20000000002'",
                        true,
                        true,
                        false,
                        function _OnSuccess(oData, oResponse){
                        var oJSON = new sap.ui.model.json.JSONModel();
                        oJSON.setData(oData);                           
                        sap.ui.getCore().setModel(oJSON, "ConnectionObject");
                        },

This is my JSON object in console log and highlighted property I want to get. I want to get every 15 Buspartner number from whole array.

enter image description here

And this is what I tried to get property:

 var oLog = sap.ui.getCore().getModel("ConnectionObject").oData.results;

 console.log(oLog);

Solution

  • If you have an array of objects, you can get an array of properties from each of these objects by using the Array.map() function.

    So in your case:

    var aResults = this.getView().getModel().getProperty("/results");
    var aBuspartner = aResults.map(function (r) { return r.Buspartner});
    var oJSONModel = new sap.ui.model.json.JSONModel();
    oJSONModel.setProperty("/resultarray", aBuspartner)