1

I am getting a simple JSON data structure with phone numbers, fax numbers and mobile numbers in a SAPUI5 application:

{
    "teles": [
                {"tele": "05312024040", "default": true},
                {"tele": "017666254336", "default": false},
                {"tele": "017666224336", "default": false}
    ],
    "faxs": [
                {"fax":"053155599755", "default": true},
                {"fax":"01548568745", "default": false}
    ],
    "mobils": [
                {"mobil":"017655994816", "default": true},
                {"mobil":"01548568745", "default": false}
    ]
 }

My goal is to loop/parse through the Data structure and get the number that is default, and put it into another model with the name "tele-standard", "fax-standard" or "mobil-standard". This should happen before displaying the data, so It can be put into these TextFields, that are editable:

Four TextFields with an option to open a dialog to edit.

I don't know at all how to do that with jquery, or sapui5 or javascript so I can reach my goal. Can you maybe help?

Results in the new model should look like:

{
    "archived": "-", 
    "nummer": "12", 
    "anrede": "Herr",
    "vorname": "Osamah", 
    "nachname": "Al-Doaiss",
    "tele-default": "05312024040",
    "fax-defaule": "053155599755",
    "mobil-default": "017655994816"
}

TL;DR: I have a JSON with Arrays. I want to parse through them and get the default number. No idea how!

1

4 Answers 4

2

Try this

1

var data = {
    "teles": [
        {"tele": "05312024040", "default": true},
        {"tele": "017666254336", "default": false},
        {"tele": "017666224336", "default": false}
    ],
    "faxs": [
        {"fax":"053155599755", "default": true},
        {"fax":"01548568745", "default": false}
    ],
    "mobils": [
        {"mobil":"017655994816", "default": true},
        {"mobil":"01548568745", "default": false}
    ]
};

var model = {
    "archived": "-", 
    "nummer": "12", 
    "anrede": "Herr",
    "vorname": "Osamah", 
    "nachname": "Al-Doaiss"
};

Object.keys(data).forEach(function (key) {
    data[key].forEach(function (el) {
        var keys = Object.keys(el),
            key  = keys.join('-');

        if (el.default && el.default === true && !model[key]) {
            model[keys.join('-')] = el[keys[0]];
        }
    });
});

console.log(model);

2

var data = {
    "teles": [
        {"tele": "05312024040", "default": true},
        {"tele": "017666254336", "default": false},
        {"tele": "017666224336", "default": false}
    ],
    "faxs": [
        {"fax":"053155599755", "default": true},
        {"fax":"01548568745", "default": false}
    ],
    "mobils": [
        {"mobil":"017655994816", "default": true},
        {"mobil":"01548568745", "default": false}
    ]
};

var model = {
    "archived": "-", 
    "nummer": "12", 
    "anrede": "Herr",
    "vorname": "Osamah", 
    "nachname": "Al-Doaiss"
};

Object.keys(data).forEach(function (key) {
    data[key].forEach(function (el) {
        var keys = Object.keys(el),
            key  = keys.join('-');

        if (el.default && el.default === true && !model[key]) {
            model[key] = el[keys[0]];
        }
    });
});

console.log(model);

Sign up to request clarification or add additional context in comments.

Comments

2

see this

var numbers = {
    "teles": [
                {"tele": "05312024040", "default": true},
                {"tele": "017666254336", "default": false},
                {"tele": "017666224336", "default": false}
    ],
    "faxs": [
                {"fax":"053155599755", "default": true},
                {"fax":"01548568745", "default": false}
    ],
    "mobils": [
                {"mobil":"017655994816", "default": true},
                {"mobil":"01548568745", "default": false}
    ]
 };

function getAllDefaultNumbers(nums) {
    var outObj = {};
    (Object.keys(nums) || []).forEach(function (number_type) {
        (nums[number_type] || []).forEach(function (obj) {
            if (obj.default) {
                outObj[number_type + '-default'] = obj.tele || obj.fax || obj.mobil;
            }
        });
    });
    return outObj;
}

document.getElementById('out').innerHTML = JSON.stringify(getAllDefaultNumbers(numbers));
<div id="out"></div>

Comments

1

Consider data to be your object :

for(var phone in data.teles) {
    if(phone.default == true) {
        // Example on how to set phone number with jquery
        $('#defaultPhone').val(phone.tele);
        // You would need an input with the id defaultPhone for this
    }
}

Edit: with regards to Your update you would probably to somethin like this (consider variable result as your results object)

result.tele-default = phone.tele //instead of the jquery part in the upper example

Comments

1

Below code snippet is for getting the default telephone number. vals will store your JSON data.

     objJson =   JSON.parse(vals);
      var tele_default;
      if(objJson.teles.count>0){
        for(int i=0;i<objJson.teles.count;i++){
            if(objJson.teles[i].default == "true"){
                tele_default = objJson.teles[i].tele
             }
          }
      }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.