How to extract values from object arguments?

How to extract values from object arguments?
0.0 0

#1

Hi,

I am trying to create a function like this –
Where i want to check if the argument is string(or a property with value as string) print name and if it finds a number print age , if it finds both print both, also it is not necessary that the arguments need to be in object form , they can be like –

displayName("ron", 25);

I just can’t seem to be able to do it , can any one kindly help … thanks

function displayName() {
    for(var i = 0; i < arguments.length; i++){
        if(typeof arguments[i] === "string"){
            console.log("name is " + arguments[i]);
        }
        else if(typeof arguments[i] === "number"){
            console.log("age is " + arguments[i]);
        }
    }
}

displayName({name: "ron", age: 25});

#2

I do not know what i did , i just kept trying what i learnt earlier and came with this crazy code and it seems to work, can any one please tell me if my thinking is right …



function displayName() {
    for(var i = 0; i < arguments.length; i++){
        if(typeof arguments[i] === "string"){
            console.log("name is " + arguments[i]);
        }
        else if(typeof arguments[i] === "number"){
            console.log("age is " + arguments[i]);
        }
        
        else if (typeof arguments[i] === "object"){
            for(var x in arguments[i]){
                if(arguments[i].hasOwnProperty(x)){
                   if(typeof arguments[i][x] === "string") {
                       console.log("name is " + arguments[i][x]);
                   } 
                    if(typeof arguments[i][x] === "number") {
                       console.log("age is " + arguments[i][x]);
                   }
                }
            }
        }
    }
}
//displayName({name: "ron", age: 25});
displayName("ron",25);

#3

arguments contains only one item and it is an object.

for…in will allow you to iterate over the properties of an array.


#4

@ArielLeslie - thanks i just posted this , will this be ok , kindly guide –


function displayName() {
    for(var i = 0; i < arguments.length; i++){
        if(typeof arguments[i] === "string"){
            console.log("name is " + arguments[i]);
        }
        else if(typeof arguments[i] === "number"){
            console.log("age is " + arguments[i]);
        }
        
        else if (typeof arguments[i] === "object"){
            for(var x in arguments[i]){
                if(arguments[i].hasOwnProperty(x)){
                   if(typeof arguments[i][x] === "string") {
                       console.log("name is " + arguments[i][x]);
                   } 
                    if(typeof arguments[i][x] === "number") {
                       console.log("age is " + arguments[i][x]);
                   }
                }
            }
        }
    }
}

//displayName({name: "ron", age: 25});
displayName("ron",25);

#6

@Dragomara -thanks, i posted a solution above its working , can you have a look and suggest if its correct


#7

Because Javascript does not have function overloading, such tasks can be exhausting to implement.
There may be a shorter solution if ES6 features are used.


#8

It’ll work fine, but why you’re trying to do this? Polymorphism (in this case a function that accepts different types) via typechecking arguments makes for complex and fragile code. The function should take an object or it should take seperate arguments, not both. Accepting either makes it hard to use and difficult to debug, particularly if you’re not defining any parameters for the function. If there’s a good reason to do this, then fair enough, otherwise it’s a code smell -

eg (assuming age > 0)

if an object

function displayName(person) {
  if (person.name) console.log('name is ' + person.name);
  if (person.age) console.log('age is ' + person.age);
}

or, better, more explicit if it’s an object

function displayName({name, age}) {
  if (name) console.log('name is ' + name);
  if (age) console.log('age is ' + age);
}

Or if seperate arguments:

function displayName(name, age) {
  if (name) console.log('name is ' + name);
  if (age) console.log('age is ' + age);
}

#9

What you’re trying to do is much like the Profile Lookup challenge in the Basic Javascript section. That exercise is a bit more complex, but you might take a look at the Get a Hint explanation.


#10

Good example of what your code should look like. Pass in parameters, act on them with a specific purpose in mind.


#11

I would first transform the arguments into a proper array with Array.prototype.slice.call(arguments), then determine if the length of the created array is 1 or 2. If understand your question correctly, if it is 1, then you can do a type check and determine if it is an object and if it is 2 you can check if the first element in the argument array is a string and so on.


#12

@amitshrivastavafcc

I am with you.

-WWC


#13

@DanCouper - thanks , i had an exercise where i need to do this and i agree with you that it can create problems …