gpt4 book ai didi

javascript - 遍历 JSON - 特殊方式

转载 作者:可可西里 更新时间:2023-11-01 12:55:53 24 4
gpt4 key购买 nike

我有一个非常大的嵌套 JSON 对象,如下所示:

let myData = {
character: {
player: {
player_1: { type: "player1", test:"A" },
player_2: { type: "player2", test:"B" },
player_3: { type: "player3", test:"C" }
},
enemy: {
enemy_walk: {
enemy_walk_1: { type:"enemy_walkA", test: "..." },
enemy_walk_2: { type:"enemy_walkB", test: "..." },
enemy_walk_3: { type:"enemy_walkY", test: "..." }
}
}
},
blocks: {
wall: {
wall_1: { type:"wallA", test: "..." },
wall_2: { type:"wallB", test: "..." },
},
obstacle: {
brick: {
brick1: { type:"brickA", test: "..." },
brick2: { type:"brickC", test: "..." },
}
}
}
}

... 我想遍历每个子树以获得这样的列表(其中每个子树的最后一个对象都有一个名为 src 的全新属性,代表 对象的路径作为字符串:

let result = {
character: {
player: {
player_1: { type: "player1", test:"A", src: "character/player/player_1" },
player_2: { type: "player2", test:"B", src: "character/player/player_2" },
player_3: { type: "player3", test:"C", src: "character/player/player_3" }
},
enemy: {
enemy_walk: {
enemy_walk_1: { type:"enemy_walkA", test: "...", src: "character/enemy/enemy_walk_1" },
enemy_walk_2: { type:"enemy_walkB", test: "...", src: "character/enemy/enemy_walk_2" },
enemy_walk_3: { type:"enemy_walkY", test: "...", src: "character/enemy/enemy_walk_3" }
}
}
},
blocks: {
wall: {
wall_1: { type:"wallA", test: "...", src: "blocks/wall/wall_1" },
wall_2: { type:"wallB", test: "...", src: "blocks/wall/wall_2" },
},
obstacle: {
brick: {
brick1: { type:"brickA", test: "...", src: "blocks/obstacle/brick/brick1" },
brick2: { type:"brickC", test: "...", src: "blocks/obstacle/brick/brick2" },
}
}
}
}

因为我真的不知道如何开始这段代码,所以到目前为止我只知道这些。

var myData={character:{player:{player_1:{type:"player1",test:"A"},player_2:{type:"player2",test:"B"},player_3:{type:"player3",test:"C"}},enemy:{enemy_walk:{enemy_walk_1:{type:"enemy_walkA",test:"..."},enemy_walk_2:{type:"enemy_walkB",test:"..."},enemy_walk_3:{type:"enemy_walkY",test:"..."}}}},blocks:{wall:{wall_1:{type:"wallA",test:"..."},wall_2:{type:"wallB",test:"..."}},obstacle:{brick:{brick1:{type:"brickA",test:"..."},brick2:{type:"brickC",test:"..."}}}}};

let updateSRC = function(data) {
let _data = data;
let recursive = function(_data) {
for (let key in _data) {
if (typeof _data[key] == "Object") {
recursive(_data[key]);
} else {
_data[key].src = "?"
}
}; recursive(_data)
}; return _data || null;
}
let result = updateSRC(myData);
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }

最佳答案

我只想创建一个带有标志(此处为 leaf)的递归函数,以指示您是否还有更多嵌套对象。

您可以跟踪在何处使用函数的参数(路径)。在每个递归步骤中,只需将键添加到路径中即可。

这将在适当的位置改变对象,但如果您要这样做的话,使用相同的技术创建新对象应该不难。

let myData = {character: {player: {player_1: { type: "player1", test:"A" },player_2: { type: "player2", test:"B" },player_3: { type: "player3", test:"C" }},enemy: {enemy_walk: {enemy_walk_1: { type:"enemy_walkA", test: "..." },enemy_walk_2: { type:"enemy_walkB", test: "..." },enemy_walk_3: { type:"enemy_walkY", test: "..." }}}},blocks: {wall: {wall_1: { type:"wallA", test: "..." },wall_2: { type:"wallB", test: "..." },},obstacle: {brick: {brick1: { type:"brickA", test: "..." },brick2: { type:"brickC", test: "..." },}}}}

function walk(obj, path=''){
let leaf = true
Object.keys(obj).forEach(key => {
if (typeof obj[key] === 'object' ){
walk(obj[key], path + '/' + key)
leaf = false // this object has children, so don't add `src` prop
}
})
if (leaf) obj['src'] = path
}

walk(myData)
console.log(myData)

关于javascript - 遍历 JSON - 特殊方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52238255/

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