# 前端面试相关

# 获取json键值映射

let json = {
	"name": "server",
	"version": "0.0.0",
	"private": true,
	"main": "server.js",
	"scripts": {
		"start": "node ./bin/www"
	},
	"dependencies": {
		"cookie-parser": "~1.4.3",
		"debug": "~2.6.9",
		"express": "~4.16.0",
		"http-errors": "~1.6.2",
		"jade": "~1.11.0",
		"morgan": "~1.9.0",
        "arr":[1,2,3],
        "test":{
            'demo':''
        }
	}
}

function getJSONKeysMapping(json){
    let result = []
    let entries = Object.entries(json);
    for(let i in entries){
        if(entries[i][1]&&entries[i][1].constructor !== Array &&typeof entries[i][1] === 'object'){
            let arr = getJSONKeysMapping(entries[i][1])
            arr = arr.map(item=>{
                return `${entries[i][0]}.${item}`
            })
            result =  result.concat(arr)
            
        }else{
            result.push(entries[i][0])
        }
    }
    return result
}
getJSONKeysMapping(json)
Last Updated: 12/14/2022, 10:58:59 PM