简体   繁体   中英

How to push key/values in object?

I'm just calling function with argument value is an Object and check if value is available into object push that's value into created object.

this is my code: calling function here:

profile({"first_name":'kaushik', "last_name":'makwana'});

declare function here:

function profile(profileInfo){
    profileInfo = JSON.stringify(profileInfo);
    var profileData  = JSON.parse(profileInfo);
    var first_name   = profileData.first_name;
    var last_name    = profileData.last_name;
    var email        = profileData.email;
    var message      = profileData.message;

    var data = {};
    if(typeof first_name != 'undefined'){
        data['first_name'] = first_name;
    }
    else if(typeof last_name != 'undefined'){
        data['last_name'] = last_name;
    }
    else if(typeof email != 'undefined'){
        data['email'] = email;

    }
    else if(typeof message != 'undefined'){
        data['message'] = message;

    }
    console.log(data);
 }

its display like:

  Object({"first_name" : kaushik});

The problem is that only one body of if - else if - else conditional is run.

So you don't want to have a bunch of else if but separated if s

Problem is you are using if and else if

So once

if(typeof first_name != 'undefined'){
        data['first_name'] = first_name;
    }

this condition is true it will ignore other conditions. so use it like

function profile(profileInfo){
    profileInfo = JSON.stringify(profileInfo);
    var profileData  = JSON.parse(profileInfo);
    var first_name   = profileData.first_name;
    var last_name    = profileData.last_name;
    var email        = profileData.email;
    var message      = profileData.message;

    var data = {};

    if(typeof first_name != 'undefined'){
        data['first_name'] = first_name;
    }
    if(typeof last_name != 'undefined'){
        data['last_name'] = last_name;
    }
    if(typeof email != 'undefined'){
        data['email'] = email;

    }
    if(typeof message != 'undefined'){
        data['message'] = message;

    }
    console.log(data);
 }
profile({"first_name":'kaushik', "last_name":'makwana'});

I hope it will help

 profileInfo = JSON.stringify(profileInfo); var profileData = JSON.parse(profileInfo); 

This is completely useless and superfluous. Why JSON encode to then decode it again? It doesn't do anything of use here.

 var first_name = profileData.first_name; 

This is also rather superfluous. Why assign each property to an individual variable? You can use the property directly as is.

 if(typeof first_name != 'undefined') 

This is questionable and depends on what exactly you expect to be a valid value. If you accept 0 as a valid first name, then you indeed need to check for undefined . However, if any falsey value should be rejected, just check for == false . The typical idiom in Javascript is:

a = a || b;

This keeps a if a is truthy , else uses b .


I'd suggest this:

function profile(profileInfo) {
    var data = {
        first_name: profileInfo.first_name || null,
        last_name: profileInfo.last_name || null,
        email: profileInfo.email || null,
        message: profileInfo.message || null,
    };
    console.log(data);
}

Note that this means data will always have all properties, in opposition to what you're doing now. But really, what are you doing?! You're just recreating the same object again. This code here essentially does the same thing:

function profile(profileInfo) {
    var data = profileInfo;
    console.log(data);
}

If you're interested in cloning the object so as to break any reference to the original, see What is the most efficient way to deep clone an object in JavaScript? . But that doesn't necessarily seem to be what you really need.

If you want to filter profileInfo to some defined sub-set (ie you want to remove all properties except a few expected ones):

function profile(profileInfo) {
    var data = {};
    for (var prop in profileInfo) {
        if (['first_name', 'last_name', 'email', 'message'].indexOf(prop) > -1) {
            data[prop] = profileInfo[prop];
        }
    }
    console.log(data);
}

remove unnecessory if-else.

function profile(profileInfo) {

            profileInfo = JSON.stringify(profileInfo);
            var profileData = JSON.parse(profileInfo);
            var first_name = profileData.first_name;
            var last_name = profileData.last_name;
            var email = profileData.email;
            var message = profileData.message;

            var data = {};

            if (typeof first_name != 'undefined') {
                data['first_name'] = first_name;
            } 
            if (typeof last_name != 'undefined') {
                data['last_name'] = last_name;
            }  
            if (typeof email != 'undefined') {
                data['email'] = email;

            } 
           if (typeof message != 'undefined') {
                data['message'] = message;

            }
            console.log(data);
        }

If you have array then you can do it in this way also.

var data = [];
        function profile(profileInfo) { 

             data.push(profileInfo);

             console.log(data);
        }


        profile({
            "first_name": 'kaushik',
            "last_name": 'makwana'
        });

        profile({
            "first_name": 'kaushik111',
            "last_name": 'makwana111'
        });

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