简体   繁体   English

访问嵌套的 JSON object 而不在 JavaScript 中指定键

[英]Access nested JSON object without specifying key in JavaScript

I decided to learn more on JavaScript looping and mapping functions by doing nested objects/arrays.我决定通过嵌套对象/数组来了解更多关于 JavaScript 循环和映射函数的信息。 Now I stumbled into a problem which is somewhat similar to this but far more complicated.现在我偶然发现了一个与此有些相似但要复杂得多的问题。

Accessing elements of JSON object without knowing the key names在不知道键名的情况下访问 JSON object 的元素

here's my JSON:这是我的 JSON:

"employees": {
    "Gill Bates": {
        "position": "Software Engineer",
        "office": "Tokyo, Japan"
    },
    "Jebron Lames": {
        "position": "Full-stack Developer",
        "office": "Manila, Philippines"
    }
}

and my accessJson function which is the famous solution for nested JSON objects:和我的accessJson function 这是嵌套 JSON 对象的著名解决方案:

 function accessJson(obj) { const result = []; for (const prop in obj) { const value = obj[prop]; if (typeof value === 'object') { result.push(toArray(value)); } else { result.push(value); } } return result; }

The code is working perfectly fine in accessing the JSON object.该代码在访问 JSON object 时运行良好。 But the problem is that I'm getting a return which looks like this:但问题是我得到的回报是这样的:

在此处输入图像描述

In which I needed to write another function again in order to trim and combine those characters just to get every employees position.我需要在其中再次编写另一个 function 以修剪和组合这些字符,以获得每个员工 position。 Is there a way I can get every employees position without specifying their name?有没有办法让每个员工 position 不指定他们的名字? Like a one line variable declaration(not working) like this:像这样的单行变量声明(不工作):

 let position = Object.keys(employees)[0].position;

In which I can insert it inside my loop so I can delete my existing accessJson function and lessen my code.我可以将它插入到我的循环中,这样我就可以删除我现有的accessJson function 并减少我的代码。

EDIT:编辑:

Please don't mark my question as a duplicate one.请不要将我的问题标记为重复的问题。 I think I already mentioned it above that my code is fine and perfectly working.我想我已经在上面提到了我的代码很好并且可以正常工作。 What I wanted to get feedback is, can we optimize my code into a one line declaration so we can get rid of accessJson function and avoid the creation of another function that will trim and combine those characters just to get every employees position.我想得到反馈的是,我们能否将我的代码优化为一行声明,这样我们就可以摆脱accessJson function 并避免创建另一个 function 来修剪和组合这些字符以获取每个员工

My question and accessJson function is similar to this:我的问题和accessJson function 与此类似:

How can I access and process nested objects, arrays or JSON? 如何访问和处理嵌套对象 arrays 或 JSON?

 const data = { "employees": { "Gill Bates": { "position": "Software Engineer", "office": "Tokyo, Japan" }, "Jebron Lames": { "position": "Full-stack Developer", "office": "Manila, Philippines" } } }; const { employees } = data; const extract = (p) => Object.keys(employees).map(name => employees[name][p]); console.log('get every employees position', extract('position')); console.log('get every employees office', extract('office'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM