简体   繁体   中英

Dynamically inserting values while iterating over JSON object in Javascript

I've gone too far down the rabbit hole for my skill level perhaps.

This is the JSON data I'm getting (it is a couple levels down.)

field_user_skill: {
    und: {
        0: {
            field_skill: { 
                und: "JavaScript" 
            },
            field_user_skill_current_rating: {
                und: [{
                    value: "5"
                }]
            },
            field_user_skill_desired_rating: {
                und: [{
                    value: "9"
                }]
            },
            remove_button: "Remove",
            _weight: 0
        },
        1: {
            field_skill: {
                und: "Ruby"
            },
            field_user_skill_current_rating: {
                und: [{
                    value: "6"
                }]
            },
            field_user_skill_desired_rating: {
                und: [{
                    value: "10"
                }]
            },
            remove_button: "Remove",
            _weight: 1
        },
    }

I'm trying to wrap my head around how I can use specific value (from an array) eg. var index = [0,1,2,3] to iterate over the object and replace the IDs at each pass, and hopefully create a NEW array where i can compare the values of field_user_skill_current_rating , and field_user_skill_desired_rating .

For example, on the first pass, use the first index of field_user_skill.und.0 , and then on the next pass using field_user_skill.und.1 , and so on and so forth for each successive pass so I can create new arrays based on the results from each one.

I tried all the (crazy) ideas that popped into my head, but right now I'm stuck.

I'm just not really sure how to REPLACE values when iterating through using Javascript. In fact, is that even possible? I'm sorry for the formatting. Thanks in advance!

This code assumes your JSON never deviates from the above structure, so you should make sure this is the case, otherwise you'll need to add the proper existence checks before trying to access a property or an array value. You could probably do this all in one loop, but transforming the data makes it simpler to work with later on.

Also, I wasn't clear on what exactly you planned to do with this data, so I added an example. Hopefully this should give you a good starting point to accomplish what your after.

Live Demo

JS

var field_user_skill =  {
    und: {
        0: {
            field_skill: { 
                und: "JavaScript" 
            },
            field_user_skill_current_rating: {
                und: [{
                    value: "5"
                }]
            },
            field_user_skill_desired_rating: {
                und: [{
                    value: "9"
                }]
            },
            remove_button: "Remove",
            _weight: 0
        },
        1: {
            field_skill: {
                und: "Ruby"
            },
            field_user_skill_current_rating: {
                und: [{
                    value: "11"
                }]
            },
            field_user_skill_desired_rating: {
                und: [{
                    value: "10"
                }]
            },
            remove_button: "Remove",
            _weight: 1
        },
    }
};

function processSkills(userSkills){
    var data = [],
        skillNum = null; 

    for(skillNum in userSkills.und){
        data.push({
            'skill': userSkills.und[skillNum].field_skill.und,
            'currentRating': parseInt(userSkills.und[skillNum].field_user_skill_current_rating.und[0].value,10),
            'desiredRating': parseInt(userSkills.und[skillNum].field_user_skill_desired_rating.und[0].value,10),
        });
    }
    return data; 
}

function doYourComparison(data){

    var i = 0, 
        count = data.length;

    for(; i < count; i++){
        if(data[i].currentRating < data[i].desiredRating){
            alert('needs improvement'); 
        }else{
            alert('doint great'); 
        }
    }

}

var data = processSkills(field_user_skill);
doYourComparison(data); 

Results

[
  {
    "skill": "JavaScript",
    "currentRating": 5,
    "desiredRating": 9
  },
  {
    "skill": "Ruby",
    "currentRating": 11,
    "desiredRating": 10
  }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM