简体   繁体   English

如何访问javascript对象的多个属性?

[英]How to access multiple properties of a javascript object?

If I have the array: 如果我有数组:

my_array = [{a:3,b:4,c:7},{a:5,b:8,c:6}]
property_names = ["a","c"]

How can use the property_names array against my_array to get the following output?: 如何对my_array使用property_names数组以获取以下输出?:

subset_array = [[3,7],[5,6]]
var my_array = [{a:3,b:4,c:7}, {a:5,b:8,c:6}];
var keys = ['a', 'c'];

my_array.map(function(d){
    return keys.map(function(k) {
        return d[k];
    });
});

This will give you [[3, 7], [5, 6]] and seems to be just what you want. 这会给您[[3, 7], [5, 6]] ,似乎正是您想要的。


[Obsolete initial answer; [过时的初步答案; before the OP updated his question] 在OP更新他的问题之前]

Use oa and oc - no magic involved and no kittens harmed! 使用oaoc不涉及魔术,不伤害小猫!

You could also use the [] syntax: o['a'] and o['c'] - but usually you only use it if you want to access a dynamic property name (ie a variable instead of a quotes string inside the [] ) 您也可以使用[]语法: o['a']o['c'] -但通常只有在您要访问动态属性名称(即,变量而不是[]


If you want an array with both values: 如果要同时使用两个值的数组:

var a_and_c = [o.a, o.c];

Now you can use a_and_c[0] and [1] to access those two values. 现在,您可以使用a_and_c[0][1]访问这两个值。

You can access the properties by two notations: 您可以通过两种表示法来访问属性:

Either oa and oc or o['a'] and o['c'] . oaoco['a']o['c']


EDIT 编辑

If you want to create a new array containing both values, then just do so using, eg, the code suggested by @Reid : 如果要创建一个包含两个值的新数组,则只需使用@Reid建议的代码即可:

[o.a, o.c]

2nd EDIT 第二次编辑

So just build your array up inside the map function like the following: 因此,只需在map函数内部构建数组,如下所示:

var filter = [ "a","c","f","zzz"]; var filter = [“” a“,” c“,” f“,” zzz“]; // should // 应该

subset_array = my_array.map( function(v){ var newArr = []; 子集数组= my_array.map(函数(v){var newArr = [];

for( var i=0; i<filter.length; ++i ) {
  if ( typeof v[ filter[i] ] !== 'undefined' ) {
    newArr.push( v[ filter[i] ] );
  }
}

return newArr;

} ); });

One thing to note is, however, that as long as you use primitive types (like the int values in the example) the above code will clone the properties. 但是要注意的一件事是,只要您使用基本类型(如示例中的int值),上述代码就会克隆属性。 This means, if you change a value in my_array at some point, subset_array will not be changed implicitly, but you have to recreate subset_array or adjust the value there, too. 这意味着,如果您在某个时刻更改my_array中的值, subset_array 不会隐式更改,但是您也必须重新创建subset_array或在其中调整值。

This is not a full answer, however I believe it will be of use: 这不是一个完整的答案,但是我相信它将是有用的:

var values = []
var obj = {a: 1, b: 2, c: 3}
for (var prop in obj) {
  if (obj.hasOwnValue(prop)) { // skip props in [[prototype]], no effect here
    values.push(obj[prop])
  }
}
values // -> [2, 1, 3] (in SOME UNDEFINED ORDER)

Key points: 关键点:

  1. Iterate [all] object property names for the object (but not [[prototype]] chain) 迭代对象的[所有]对象属性名称(但不迭代[[prototype]]链)
  2. Access value dynamically from property name 从属性名称动态访问值
  3. Properties are not ordered ; 属性不排序 ; sort values if needed, for instance 例如,根据需要对values进行排序

This can be expanded to add different "exclusion" rules, for instance. 例如,可以将其扩展以添加不同的“排除”规则。 It can also be adapted to different higher-order functions. 它也可以适用于不同的高阶函数。

Happy coding. 快乐的编码。

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

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