简体   繁体   English

在多级 JSON 中创建几级深的值列表

[英]Creating a list of Values that are a couple levels deep in a Multi-Level JSON

I'll lead with the fact that I'm relatively new to Javascript in general.我会以我对 Javascript 相对较新的事实来引导。 That said, I am likely not even asking the right question but I am hopeful that I'll give enough info that someone smarter in this than I will help un-screw me.也就是说,我什至可能没有问正确的问题,但我希望我能提供足够的信息,让比我更聪明的人能帮助我解开这个谜。 Thank you in advance!先感谢您!

I have an API I'm pulling info from and serving it on the DOM.我有一个 API,我正在从中提取信息并在 DOM 上提供它。 I only need a list of one of the values.我只需要其中一个值的列表。 The problem I'm having is that I can't seem to target the value correctly because it is several layers deep in the JSON.我遇到的问题是我似乎无法正确定位该值,因为它在 JSON 中有几层深。

Ideally I'll have this replace and append an existing list on the page by targeting the ID.理想情况下,我将通过定位 ID 来替换并附加页面上的现有列表。 I can get that to work, but in this code example I can only get all 81 indexes.我可以让它工作,但在这个代码示例中,我只能获得所有 81 个索引。 If I add [0] to the onetObj then I get a list of the names in that index.如果我将 [0] 添加到 onetObj 中,那么我会得到该索引中名称的列表。 What I want is the value of one of those names only, for all of the indexes, in a list.我想要的是列表中所有索引的仅这些名称之一的值。 Specifically the Alternate Titles in each index.特别是每个索引中的替代标题。

The way they have auth setup you won't be able to run my code directly without a CORS violation.他们进行身份验证设置的方式您将无法在没有 CORS 违规的情况下直接运行我的代码。 So I'm including the code I'm using as well as a copy of the array I'm working with.所以我包括了我正在使用的代码以及我正在使用的数组的副本。 I hope that helps.我希望这有帮助。

EDIT编辑

There was a question about my end goal here.这里有一个关于我的最终目标的问题。 Hopefully this will help!希望这会有所帮助!

I am building a career matching site using a CMS of info from my client and pulling in extra data from a Department of Labor API.我正在使用来自客户的信息 CMS 并从劳工部 API 获取额外数据来构建职业匹配网站。 There are over 1100 careers in their database and 37 databases of additional information for us to pick and choose from.他们的数据库中有 1100 多个职业和 37 个附加信息数据库供我们挑选。

In this specific example I have a page that has the main title of the career and a writeup on it from the client's CMS.在这个特定示例中,我有一个页面,其中包含职业的主要标题以及来自客户 CMS 的文章。 I'm trying to add a list of Alternate Titles for that career.我正在尝试添加该职业的替代头衔列表。 The Alternate Titles come from the API.替代标题来自 API。

All I can seem to get to show on the DOM is a list of the Row numbers when the only thing I need is a list of the Alternate Titles (alternate_title) from each row.当我唯一需要的是每一行的备用标题 (alternate_title) 列表时,我似乎只能在 DOM 上显示一个行号列表。

My script:我的脚本:

<script>
  
  var myHeaders = new Headers();
  myHeaders.append("Accept", "application/json");

  var requestOptions = {
    method: 'GET',
    headers: myHeaders,
    redirect: 'follow'
  };

  async function start(){
    const response = await fetch("https://services.onetcenter.org/v1.9/ws/database/rows/alternate_titles?filter=onetsoc_code.eq.17-2051.00&end=9999&client=careerpuppy", requestOptions)
    const data = await response.json()
    console.log(data)
    createOnetObj(data.row)
  }

   start()

  function createOnetObj(onetObj) {
    document.getElementById("alttitlelist").innerHTML = `
    ${Object.keys(onetObj).map(function (altTitle) {
      return `<li>${altTitle}</li>`
     }).join('')}
    `
   }

</script>

Here's two indexes from the 81 in the JSON I'm getting with this request:这是我通过此请求获得的 JSON 中 81 的两个索引:

end: 81
filter: ["onetsoc_code.eq.17-2051.00"]
info: "https://services.onetcenter.org/v1.9/ws/database/info/alternate_titles"
row: Array(81)
0: {onetsoc_code: "17-2051.00", title: "Civil Engineers", alternate_title: "Airport Engineer", short_title: null, sources: "08"}
1: {onetsoc_code: "17-2051.00", title: "Civil Engineers", alternate_title: "Architectural Engineer", short_title: null, sources: "08"} 

Your objectives are unclear to me but Object.keys is not what you need.我不清楚你的目标,但Object.keys不是你需要的。

You work with data.row, that's an Array .您使用 data.row,这是一个Array

If you want to iterate over values, use something like:如果要迭代值,请使用以下内容:

data.row.forEach( (value, index) => {
    console.log(index, ':', value.alternate_title);
});

const alternateTitles = data.row.map( value => value.alternate_title);

If you need unicity, the solution is here如果您需要唯一性, 解决方案就在这里

Edition Just put things together, something like只是把东西放在一起,像

<script>
  
  async function updateListItems(){
    const url = "https://services.onetcenter.org/v1.9/ws/database/rows/alternate_titles?filter=onetsoc_code.eq.17-2051.00&end=9999&client=careerpuppy";
    const response = await fetch(url, {
      method: 'GET',
      headers: { "Accept" : "application/json"},
      redirect: 'follow'
    });
    const data = await response.json();
    

    const list = document.getElementById("alttitlelist");
    const listItems = data.row.map( value => `<li>${value.alternate_title}</li>`).join('');
    list.innerHTML = listItems;
  }

  updateListItems();

</script>

And unicity is just what it means, removing duplicate items.唯一性就是它的意思,删除重复项。

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

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