简体   繁体   English

使用 javascript(Airtable 结构)过滤 JSON 的嵌套数组

[英]Filtering Nested Array of JSON using javascript (Airtable structure)

I'm not a javascript programmer, but I have a need to write a piece of javascript code (in an HTML page) to filter data from an Airtable JSON array fetched from an API call.我不是 javascript 程序员,但我需要编写一段 javascript 代码(在 HTML 页面中)以过滤从 API 调用获取的 Airtable JSON 数组中的数据。 I've researched this a lot and tried several options, but none of them worked for me.我对此进行了很多研究并尝试了几种选择,但没有一种对我有用。

I have a simple base on Airtable with the following data:我有一个基于 Airtable 的简单基础,包含以下数据:

    "records": [
         {
            "id": "reck1GtJ3KfhW9zEO",
            "fields": {
                "highlight": true,
                "price": 15,
                "product_name": "product 1",
                "prod_id": "G001"
            },
            "createdTime": "2021-09-21T20:01:42.000Z"
        },
        {
            "id": "rec7NJh86AK1S9kyN",
            "fields": {
                "highlight": true,
                "price": 50,
                "product_name": "product 2",
                "prod_id": "G002"
            },
            "createdTime": "2021-09-21T20:01:42.000Z"
        },
        {
            "id": "rec5JdK6pbuzsvXA1",
            "fields": {
                "price": 20,
                "product_name": "product 3",
                "prod_id": "G003"
            },
            "createdTime": "2021-09-21T20:01:42.000Z"
        },
        {
            "id": "recV39iR6tBzrmZ5s",
            "fields": {
                "price": 35,
                "product_name": "product 4",
                "prod_id": "G004"
            },
            "createdTime": "2021-09-21T20:01:42.000Z"
        }
    ]
}

The code I have is as follows:我的代码如下:

(async() => {

URL= 'https://api.airtable.com/v0/*removed*';
let response = await fetch(URL, {headers:{ "Authorization":"Bearer *removed*"},});

result = await response.json();

// FILTERATION OPTION 1
//====================
const output_data = result.records.filter(d => d.product_name == "product 1");

console.log(result);
console.log(output_data);

})();

Also tried this, didn't work也试过这个,没用


// FILTERATION OPTION 2
//====================
var output_data = result.records.filter(function (el) {
  return
  el.price == 35;
});

I want to filter the data above using prod_name or price, or any fields combination (including id) .我想使用 prod_name 或 price 或任何字段组合(包括 id)过滤上面的数据。 I'd really appreciate your help with this.非常感谢你的帮助。

Try this:尝试这个:

result.records.filter(d => d.fields.product_name == "product 1");

Its should be filter(d => d.fields.product_name == "product 1") .它应该是filter(d => d.fields.product_name == "product 1")

because your product_name key is inside your fields object of each node in the array.因为您的product_name键位于数组中每个节点的fields object 内。

 const result = {"records":[{"id":"reck1GtJ3KfhW9zEO","fields":{"highlight":true,"price":15,"product_name":"product 1","prod_id":"G001"},"createdTime":"2021-09-21T20:01:42.000Z"},{"id":"rec7NJh86AK1S9kyN","fields":{"highlight":true,"price":50,"product_name":"product 2","prod_id":"G002"},"createdTime":"2021-09-21T20:01:42.000Z"},{"id":"rec5JdK6pbuzsvXA1","fields":{"price":20,"product_name":"product 3","prod_id":"G003"},"createdTime":"2021-09-21T20:01:42.000Z"},{"id":"recV39iR6tBzrmZ5s","fields":{"price":35,"product_name":"product 4","prod_id":"G004"},"createdTime":"2021-09-21T20:01:42.000Z"}]}; console.log(result.records.filter(d => d.fields.product_name == "product 1"));

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

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