如何将对象属性作为参数传递?(JavaScript)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13760186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass an object property as a parameter? (JavaScript)
提问by maze
I'm not sure if the title is appropriate for what I am trying to achieve
我不确定标题是否适合我想要实现的目标
I am mapping JSON results to an array. Because I need to do this over and over again I would like to put the code into one function.
我正在将 JSON 结果映射到一个数组。因为我需要一遍又一遍地执行此操作,所以我想将代码放入一个函数中。
In the following examples I am repeating myself. I have properties called item.data1, item.data2 and in the second example item.something1, item.something2 ... How can I pass those properties as "general" arguments to the newly created function in order to use them there and not repeat myself to return those maps? The new function should be useable for the two examples below as well as for other cases where the properties could have different names.
在下面的例子中,我在重复我自己。我有名为 item.data1、item.data2 的属性,在第二个示例中 item.something1、item.something2 ......我如何将这些属性作为“通用”参数传递给新创建的函数,以便在那里使用它们而不是重复自己返回那些地图?新函数应该可用于下面的两个示例以及属性可能具有不同名称的其他情况。
service.getData(function(data) {
var map = {};
map = $.map(data, function(item, i) {
var entry = {};
entry.key = item.data1;
entry.value = item.data2;
return entry;
});
});
service.getSomething(function(data) {
var map = {};
map = $.map(data, function(item, i) {
var entry = {};
entry.key = item.something1;
entry.value = item.something2;
return entry;
});
});
回答by Alex Wayne
use []with a string to pull out properties from objects dynamically.
[]与字符串一起使用以动态地从对象中提取属性。
var a = { foo: 123 };
a.foo // 123
a['foo'] // 123
var str = 'foo';
a[str] // 123
Which means we can refactor your method like so, just pass in the names of the properties you want to read as strings.
这意味着我们可以像这样重构你的方法,只需传入你想要作为字符串读取的属性的名称。
var getKeyValueMap = function(data, keyPropName, valuePropName) {
return $.map(data, function(item, i) {
return {
key: item[keyPropName],
value: item[valuePropName]
}
});
};
service.getData(function(data) {
return getKeyValueMap(data, 'data1', 'data2');
});
service.getSomething(function(data) {
return getKeyValueMap(data, 'something1', 'something2');
});
回答by nozzleman
this can be done using the []operators instead of the .-notation just like in this fiddle ive created just for you :D:
这可以使用[]运算符而不是 -.符号来完成,就像我为您创建的这个小提琴一样:D:
var data = [{
data1: 'foo',
data2: 'bar',
something1: 'sparky',
something2: 'arf arf!'},
{
data1: 'My name is',
data2: 'What?',
something1: 'my name is',
something2: 'Who?!'}
];
function test(data, prop) {
var d_length = data.length;
for (var i = 0; i < d_length; i++) {
alert(data[i][prop]);
}
}
test(data, 'data1');
回答by Bergi
You will need to pass the itemobject and a map of item-keys to entry-keys. This could either be two arrays, an array of tuples or something, or an object. Most simple method I could think of is to change the descriptor object into the entry itself:
您需要将item对象和项键映射传递给输入键。这可以是两个数组、一个元组数组或其他东西,也可以是一个对象。我能想到的最简单的方法是将描述符对象更改为条目本身:
function mapPropertyNames(source, dest) {
for (var prop in dest)
dest[prop] = source[dest[prop]];
return dest;
}
// and use it like this:
var map = $.map(data, function(item, i) {
return mapPropertyNames(item, {key:"something1", value:"something2"});
});
回答by Raibaz
Something like
就像是
service.getData(function(data) {
var map = {};
var varNamePrefix = 'data';
map = $.map(data, function(item, i) {
return getProperties(item, varNamePrefix);
});
});
service.getSomething(function(data) {
var map = {};
var varNamePrefix = 'something';
map = $.map(data, function(item, i) {
return getProperties(item, varNamePrefix);
});
});
function getProperties(item, varNamePrefix) {
var ret = {};
ret.key = item[varNamePrefix + '1'];
ret.value = item[varNamePrefix + '2'];
return ret;
}
May help?
可以帮忙吗?
Basically, you could use a function that takes a property prefix and uses it to form the actual property names to get from items.
基本上,您可以使用一个函数,该函数接受一个属性前缀并使用它来形成实际的属性名称以从项目中获取。
回答by dencey
try this:
尝试这个:
service.getData(function(data, props) {// props is a property object
var map = {};
map = $.map(data, function(item, i) {
var entry = {};
for (var key in props) {
entry[key] = item[key]
}
return entry;
});
});
or use a property array
或使用属性数组
service.getData(function(data, props) {// props is like ['data1', 'data2']
var map = {};
map = $.map(data, function(item, i) {
var entry = {};
for (var j = 0, k = props.length; j < k; j++) {
entry[props[j]] = item[props[j]]
}
return entry;
});
});

