javascript - 通过对象数组循环遍历对象数组等
问题描述
我有一个通用函数,我想用任何对象结构调用它。一些对象具有对象数组,然后又具有对象数组等。
我让它工作的唯一方法就是这样,它似乎不是最聪明的方法,因为我一直在增加函数的深度。
有没有更好的办法?我尝试了一个在堆栈上找到的递归函数,但无法让它工作。
// Look for everyting at level ONE
for (var property in textfield) {
// Is there a field?
if (textfield.hasOwnProperty(property)) {
// Is level ONE an OBJECT
if (typeof textfield[property] == "object") {
// Look for everyting at level TWO
for (var subProperty in textfield[property]) {
// Is there a field?
if (textfield[property].hasOwnProperty(subProperty)) {
// Is level TWO an OBJECT
if (typeof textfield[property][subProperty] == "object") {
// Look for everyting at level THREE
for (var subSubProperty in textfield[property][subProperty]) {
// Is there a field?
if (textfield[property][subProperty].hasOwnProperty(subSubProperty)) {
// Is level THREE an OBJECT
if (typeof textfield[property][subProperty][subSubProperty] == "object") {
// Look for everyting at level FOUR
for (var subSubSubProperty in textfield[property][subProperty][subSubProperty]) {
// Translate everything at level FOUR
console.log("-----------------------LEVEL 4")
console.log("LOOP: " + textfield[property][subProperty][subSubProperty][subSubSubProperty]);
console.log("TYPE: " + typeof textfield[property][subProperty][subSubProperty][subSubSubProperty]);
textfield[property][subProperty][subSubProperty][subSubSubProperty] = replaceFields(textfield[property][subProperty][subSubProperty][subSubSubProperty]);
}
} else {
// Translate everything at level THREE
console.log("-----------------------LEVEL 3")
console.log("LOOP: " + textfield[property][subProperty][subSubProperty]);
console.log("TYPE: " + typeof textfield[property][subProperty][subSubProperty]);
textfield[property][subProperty][subSubProperty] = replaceFields(textfield[property][subProperty][subSubProperty]);
}
}
}
} else {
// Translate everything at level TWO
console.log("-----------------------LEVEL 2")
console.log("LOOP: " + textfield[property][subProperty]);
console.log("TYPE: " + typeof textfield[property][subProperty]);
textfield[property][subProperty] = replaceFields(textfield[property][subProperty]);
}
}
}
} else {
// Translate everything at level ONE
console.log("-----------------------LEVEL 1")
console.log("LOOP: " + textfield[property]);
console.log("TYPE: " + typeof textfield[property]);
textfield[property] = replaceFields(textfield[property]);
}
}
}
感谢金维珠在下面的回答,这是最终版本,它可以完美运行!!
function translateAll(textfield) {
for (var property in textfield) {
if (!textfield.hasOwnProperty(property)) {
return false;
} else if (typeof textfield[property] !== "object") {
textfield[property] = replaceFields(textfield[property]);
} else {
translateAll(textfield[property]);
}
}
}
解决方案
我不确定你想对你的对象或数组值做什么,但我希望这会对你有所帮助。以下函数打印值并在对象或数组的情况下递归调用自身:
const getValues = (value, path = '') => {
if (typeof value === 'object'
&& value !== null
&& value.length === undefined
&& Object.keys(value).length > 0) {
// It is an Object (not null, array or Date instance)
Object.keys(value).forEach((prop) => {
const _path = path.length ? `${path}.${prop}` : prop;
getValues(value[prop], _path);
});
} else if (typeof value === 'object'
&& value !== null
&& value.length > 0) {
// It is an array
value.forEach((item, idx) => {
const _path = path.length ? `${path}[${idx}]` : idx;
getValues(item, _path);
});
} else {
console.log(`${path} = ${JSON.stringify(value)}`);
}
};
例如:
const obj = {
a: 1,
b: { a: 1, b: 2 },
c: [
{ a: 1, b: 2 },
{ a: 1, b: 2 },
],
d: {},
e: [],
f: new Date(),
g: null,
h: undefined,
};
getValues(obj, 'obj');
将输出:
obj.a = 1
obj.b.a = 1
obj.b.b = 2
obj.c[0].a = 1
obj.c[0].b = 2
obj.c[1].a = 1
obj.c[1].b = 2
obj.d = {}
obj.e = []
obj.f = "2020-07-14T06:02:12.037Z"
obj.g = null
obj.h = undefined
推荐阅读
- c# - 是否可以在 Visual Studio 上按特定顺序运行单元测试类?
- amazon-web-services - 具有 aws elasticsearch 数据源的 Grafana 在 phantomjs 中不起作用
- c# - POCO 与 Entity Framework Core 2 的关系更新
- sql - JCR-SQL2 查询子节点
- asp.net - 敲除数据绑定到 ASP gridview
- sql - 选择最后一个关联对象创建时间少于 x 天的用户
- java - 无法使用 SSL 在 java 客户端和 python 服务器之间执行通信
- c# - 带有 ASP.NET MVC 5 应用程序的 Microsoft 身份验证循环
- android - 线性布局改变大小
- javascript - 在不重新加载页面的情况下替换 url 中的域