简体   繁体   English

如何从未命名的 JSON 对象数组中获取数据

[英]How to get DATA from unnamed JSON Object array

I used fetch method to get data from JSON into Object array and now I want to get only one value from object, but my array doesn't have name.我使用 fetch 方法将数据从 JSON 获取到 Object 数组中,现在我只想从对象中获取一个值,但我的数组没有名称。

JS: JS:

async function s_data() {
  let r = await fetch("terms.json");
  let d = await r.json();
  return d;
}

Array looks like数组看起来像

0: Object { TERM: "x", PRIORITY: "1" }
​​
1: Object { TERM: "y", PRIORITY: "0" }
​​
2: Object { TERM: "z", PRIORITY: "0" }
​​

And i want to paste only terms into Array.我只想将术语粘贴到数组中。

For example:例如:

async function s_data() {
  let r = await fetch("terms.json");
  let d = await r.json();
  let terms = d.map(item => item.TERM);
  return terms;
}

Or:或者:

async function s_data() {
  let r = await fetch("terms.json");
  let d = await r.json();
  let terms = d.reduce((acc, item) => {acc.push(item.TERM); return acc}, []);
  return terms;
}

Array.prototype.map() Array.prototype.map()

Array.prototype.reduce() Array.prototype.reduce()

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

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