我有一个通用函数,我想用任何对象结构来调用。一些对象具有对象数组,然后又具有对象数组等。
使它起作用的唯一方法是这样,并且它似乎并不是最聪明的方法,因为我一直在增加函数的深度。
有更好的方法吗?我尝试了在堆栈中找到的递归函数,但无法正常工作。
// 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]);
}
}
}
答案 0 :(得分:2)
我不确定您想对对象或数组值做什么,但是希望对您有所帮助。下面的函数打印值并在对象或数组的情况下递归调用自身:
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
答案 1 :(得分:2)
我不确定您的意思是否与我的理解相同。
let index = 0;
function fn(textfield) {
index++;
for (var property in textfield) {
if (!textfield.hasOwnProperty(property)) {
return false;
} else if (typeof textfield[property] !== "object") {
console.log("-----------------------LEVEL " + index);
console.log("LOOP: " + textfield[property]);
console.log("TYPE: " + typeof textfield[property]);
// textfield[property] = replaceFields(textfield[property]);
} else {
fn(textfield[property]);
}
}
}
const obj = {a:{ b: { c: { d:1} } }};
fn(obj);