我有一个对象,我想迭代该对象的某些特定键。怎么做到这一点?
考虑下面的代码段:
如何迭代table1,table2和table3键而不是所有键?
var table_object = {
table1: "hello world",
table1_name: "greetings_english.html",
table2: "hola",
table2_name: "greetings_spanish.html",
table3: "Bonjour",
table3_name: "greetings_french.html"
};

答案 0 :(得分:15)
您可以过滤密钥,然后迭代其余部分。
var table_object = { table1: "hello world", table1_name: "greetings_english.html", table2: "hola", table2_name: "greetings_spanish.html", table3: "Bonjour", table3_name: "greetings_french.html" };
Object
.keys(table_object)
.filter(function (k) { return !/_/.test(k); })
.forEach(function (k) { console.log(table_object[k]); });
答案 1 :(得分:4)
您必须指定必须迭代的键:
var table_object = {
table1: "hello world",
table1_name: "greetings_english.html",
table2: "hola",
table2_name: "greetings_spanish.html",
table3: "Bonjour",
table3_name: "greetings_french.html"
};
var keysToIterateThrough = ["table1", "table2", "table3"];
keysToIterateThrough.forEach(key => {
var value = table_object[key];
console.log(`key: ${key} => value: ${value}`);
})
答案 2 :(得分:3)
您必须使用Object.keys
才能找到所有对象键,然后应用filter
方法以过滤它们。
var table_object = {
table1: "hello world",
table1_name: "greetings_english.html",
table2: "hola",
table2_name: "greetings_spanish.html",
table3: "Bonjour",
table3_name: "greetings_french.html"
};
var keysToIterate = ["table1", "table2", "table3_name"];
let values=Object.keys(table_object)
.filter(a=>keysToIterate.includes(a))
.map(a=> table_object[a]);
console.log(values);
答案 3 :(得分:2)
可以尝试类似
的内容var keys = ['table1', 'table2', 'table3']
Object.keys(table_object).forEach(function(key) {
if (keys.indexOf(key) != -1) {
var table_value = table_object[key]
}
})
答案 4 :(得分:1)
可能无法迭代有限的键,但您可以拥有要迭代的键数组。然后遍历该数组以从对象
获取相应的值
var objKeys = ['table1', 'table2', 'table3']
var table_object = {
table1: "hello world",
table1_name: "greetings_english.html",
table2: "hola",
table2_name: "greetings_spanish.html",
table3: "Bonjour",
table3_name: "greetings_french.html"
};
objKeys.forEach(function(item) {
console.log(table_object[item])
})

答案 5 :(得分:1)
我认为在这种情况下更好的方法就是让你的对象像这样:
var table = {
"hello world": "greetings_english.html",
"hola": "greetings_spanish.html",
"bonjour": "greetings_french.html"
};
for( var i in table ) {
console.log( i );
console.log( table[ i ] );
}
或者你可以创建两个数组:
var greetings = [
"hello world",
"hola",
"bonjour"
];
var names = [
"greetings_english.html",
"greetings_spanish.html",
"greetings_french.html"
];
for( var i = 0; i < greetings.length; i ++ ) {
console.log( greetings[ i ] );
console.log( names[ i ] );
}
您可以使用此方法制作表格
但无论如何你的问题:
var table = {
table1: "hello world",
table1_name: "greetings_english.html",
table2: "hola",
table2_name: "greetings_spanish.html",
table3: "bonjour",
table3_name: "greetings_french.html"
};
// Now there are three methods
console.log( "--- Method 1 ---" );
Object.keys( table )
.filter( function( key ) {
return /^table(\d+)$/.test( key );
} )
.forEach( function( key ) {
console.log( key );
console.log( table[ key ] );
console.log( table[ key + "_name" ] );
} );
console.log( "--- Method 2 ---" );
for ( var i in table ) {
if ( /^table(\d+)$/.test( i ) ) {
console.log( i );
console.log( table[ i ] );
console.log( table[ i + "_name" ] );
}
}
console.log( "--- Method 3 ---" );
var keys = [
"table1",
"table2",
"table3"
];
for ( var i = 0; i < keys.length; i ++ ) {
console.log( keys[ i ] );
console.log( table[ keys[ i ] ] );
console.log( table[ keys[ i ] + "_name" ] );
}
方法2将是最好的。
答案 6 :(得分:1)
您不需要花哨的过滤器或正则表达式来完成这么简单的任务!忽略那些误导性的答案,并开始使用JavaScript的全部功能!
您应该在对象上使用Object.defineProperty()
方法,并设置为enumerable: false
您不想迭代的所有属性。通过这种方式,您还可以将命名约定与逻辑分离。让我告诉你:
// Defining iterable properties. This ones will be returned
// by Object.keys()
var table_object = {
table1: "hello world",
table2: "hola",
table3: "Bonjour",
// It works even if you declare them in advance
// table1_name: "greetings_english.html",
// table2_name: "greetings_spanish.html",
// table3_name: "greetings_french.html",
};
// Declaring the not iterable properties. They can still be
// accessed, but they will not be iterated through
Object.defineProperty(table_object, "table1_name", {
// delete the next line if you have already declared it
value: "greetings_english.html",
enumerable: false
});
Object.defineProperty(table_object, "table2_name", {
// delete the next line if you have already declared it
value: "greetings_spanish.html",
enumerable: false
});
Object.defineProperty(table_object, "table3_name", {
// delete the next line if you have already declared it
value: "greetings_french.html",
enumerable: false
});
// Iterating through the object using for ... in, which iterates
// over the keys
for (var key in table_object) {
console.log(table_object[key]);
}
仍然可以使用Object.getOwnPropertyNames()
检索不可枚举的属性以及所有可枚举的属性。
但是,如果您打算在不再需要过滤时实际使用第二种方法迭代所有属性,我会给你一些建议:
enumerable
可以设置回true
,因此,如果是一次性更改,我强烈建议您将其还原。
如果过滤来回频繁,那么你应该检查对象结构的两倍,因为可能有更合适的选项满足你的需求(例如数组到对象中)。