gpt4 book ai didi

javascript - 在 "name"中递归搜索具有键值对的所有值的 JSON 字符串字典

转载 作者:行者123 更新时间:2023-11-29 18:18:31 25 4
gpt4 key购买 nike

<分区>

更新:

更新 fiddle :http://jsfiddle.net/Y2xFV/10/

I accepted one of the below answers although both offered are satisfactory. The one I selected is closer to the direction I myself took. Neither of the offered solutions actually do exactly what I need however.

在实践中,我最终编写了满足我实际需要的 angular-js 函数,看起来像这样......

function getNames(obj) {
if(obj.attrs) {
console.log(obj.attrs.name);
// expressions that utilize key pair values..
}
if(!obj.children) {
return;
}
angular.forEach(obj.children, function(o) {
getNames(o);
});
}

感谢大家的意见和建议。

=========================================

我下面有一个 JSON 字符串,我必须获取所有名称的值。我知道有一种递归方法可以做到这一点,但我需要帮助。我在堆栈中搜索了解决此问题的方法,但没有找到任何可以直接解决它的方法。

我在上类。我会尽快更新 fiddle 。我的时间非常有限,有时我不会立即回到我的堆栈中。如果这对某人来说是个问题,我深表歉意。然而,我不需要毫秒内的答案,我接受建设性的批评 :)。一旦我真正设法找到 fiddle ,我就会发布问题。

[
{
"name": "john",
"surname": "doe",
"children": [
{
"name": "anne",
"surname": "peters"
}
]
},
{
"name": "joe",
"surname": "doe",
"children": [
{
"name": "bob",
"surname": "peters",
"children": [
{
"name": "bill",
"surname": "peters"
}
]
}
]
},
{
"name": "jim",
"surname": "doe"
}
]

我需要得到:

约翰安妮乔鲍勃账单吉姆

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com