如何对 javascript 对象中 属性 的每个实例的值执行函数?

How can I perform a function onto the value of each instance of a property in a javascript object?

我有一个这样的对象。它将以这种格式继续使用越来越多的子块,但这个片段给出了一个想法。我正在尝试用 NLP 解析版本替换整个对象中的数据值。

{
"date": "next friday"
"more text": "someVariable"
"children": [{
    "date": "today"
    "more text": "someVariable"
    "children": [{
        "date": "yesterday"
        "more text": "someVariable"
        "children": []}, 
        {"date": "yesterday"
        "more text": "someVariable"
        "children": []}] 
}
}

我想做的是 运行 将文本替换函数 整个数组中的“日期”值和 return 一个变异的对象,只要查找和 replcae 操作成功,除了“日期”的值外,一切都保持不变。

我本质上是 运行NLP 对项目“日期”的值

我尝试过递归函数,但不知道如何保持原来的结构。

async function parseChildren(obj, child = true){
    for (var k in obj)
    {if (obj[k] !== null){
        // console.log(obj(k))
        if (k == "date"){
            console.log(obj[k])
            console.log(parseDates(obj[k]))
        }
        if (k == "children"){
            parseChildren(obj[k], false)
        }
        else if (child == false) {
        parseChildren(obj[k])
        }
        
    }
}
}

提前感谢您的帮助!

您可以尝试这样的操作:

const test = {
    "date": "next friday",
    "more text": "someVariable",
    "children": [{
        "date": "today",
        "more text": "someVariable",
        "children": [{
            "date": "yesterday",
            "more text": "someVariable",
            "children": []}, 
            {"date": "yesterday",
            "more text": "someVariable",
            "children": []}] 
    }]
    }

function mutateDates(obj){
    if (obj.date) {
        obj.date = 'mutation_here_we_it_is'
    }

    obj.children.map(mutateDates)
}

mutateDates(test)

这将导致:

{
    "date": "mutation_here_we_it_is",
    "more text": "someVariable",
    "children": [{
        "date": "mutation_here_we_it_is",
        "more text": "someVariable",
        "children": [{
            "date": "mutation_here_we_it_is",
            "more text": "someVariable",
            "children": []
        }, {
            "date": "mutation_here_we_it_is",
            "more text": "someVariable",
            "children": []
        }]
    }]
}