二话不说,直接看代码。

方式一:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
const config = {
  a: { name: "name", value: 1 },
  b: { name: "name2", value: 2 },
};

for (const name of Object.getOwnPropertyNames(config)) {
  const property = config[name];
  // 过滤方法和_开头的私有属性
  if (property instanceof Function || name.indexOf("_") >= 0) {
    continue;
  }
  console.log(config.constructor.name, name, property.value);
}

输出: Object a 1 Object b 2

方式二:

1
2
3
4
5
6
7
8
9
const config = {
  a: { name: "name", value: 1 },
  b: { name: "name2", value: 2 },
};

for (const index in config) {
  const item = config[index];
  console.log(item);
}

输出: {name: “name”, value: 1} {name: “name2”, value: 2}