简体   繁体   English

使用javascript函数参数返回对象值

[英]Using javascript function argument to return object value

Javascript function that takes a single argument. 带有单个参数的Javascript函数。 Use that argument value which is a string to return the appropriate value from the matched object key. 使用该参数值(是字符串)从匹配的对象键返回适当的值。

 function someFunction(someArg) {

var message = {
    bob: "Hello bob",
    mike: "Hello mike",
    tara: "Hello tara"
}

console.log(message +  " " + message.someArg + " " + someArg + " " + message.bob);

} }

what is returned is 返回的是

 [object Object] undefined bob Hello bob

Where undefined is returned in the console log, JavaScript should return the message "Hello bob" as the value of someArg is "bob", calling message.bob returns the correct result. 在控制台日志中返回undefined的地方,JavaScript应该返回消息“ Hello bob”,因为someArg的值为“ bob”,调用message.bob返回正确的结果。

To print it properly, you'll have to: 要正确打印,您必须:

  • Stringify the message object 字符串化消息对象
  • Refer to the property of message in a correct manner 以正确的方式引用消息的属性

Try this 尝试这个

function someFunction(someArg) {
   var message = {
    bob: "Hello bob",
    mike: "Hello mike",
    tara: "Hello tara"
   }
   //ES6
   console.log(`${JSON.stringify(message)} ${message[someArg]} ${someArg} ${message.bob}`);
   //ES5
   console.log(JSON.stringify(message) +  " " + message[someArg] + " " + someArg + " " + message.bob);

}

Now, on calling someFunction('bob') , the output is: 现在,在调用someFunction('bob')时 ,输出为:

{"bob":"Hello bob","mike":"Hello mike","tara":"Hello tara"} Hello bob bob Hello bob

You have to use the [] notation, where obj[key] is the same as obj.key, but key can be a variable. 您必须使用[]表示法,其中obj [key]与obj.key相同,但是key可以是变量。

 function someFunction(someArg) { var message = { bob: "Hello bob", mike: "Hello mike", tara: "Hello tara" } console.log(JSON.stringify(message) + " " + message[someArg] + " " + someArg + " " + message.bob); } someFunction("mike"); 

When using message.someArg you are "telling" attribute someArg or your message Object. 使用message.someArg您是在“告诉”属性someArg或您的消息对象。

What you have to use is message[someArg] to get the dynamic property. 您需要使用message[someArg]来获取动态属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Javascript中将函数作为参数传递时返回值 - Return value when passing a function as an argument in Javascript javascript function 不会返回值或 object - javascript function will not return value or object javascript返回值绑定为对象值的函数值 - javascript return value of function binded as object value javascript如何从参数函数中获取函数返回值 - javascript how to get function return value out of argument function 是否有 Javascript function 以 object 作为参数并返回字符串数组 - Is there a Javascript function to take an object as argument and return an array of strings 对象的Javascript函数作为参数 - Javascript function of object as argument javascript函数搜索对象属性并返回值 - javascript function to search for object property and return value 使用高阶函数,如果另一个值为true,则返回一个对象值(JavaScript) - Using a higher order function, return one object value if another value is true (JavaScript) 如果参数是数字,我怎样才能让雪花中的 javascript 函数返回一个值,如果参数不是数字,我如何返回另一个值? - How can I get a javascript function in snowflake to return one value if the argument is numeric or another value if the argument is not numeric? Javascript 对象:如何使用值返回属性? - Javascript object: How to return property using value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM