API接口获得数据后处理JS数组(包含字符串对象)分组、过滤和筛选的解决方案

简介: API接口获得数据后处理JS数组(包含字符串对象)分组、过滤和筛选的解决方案

读取API后,数组多是以字符串对象的形态存在于数组中的。根据实际情况,需要对得到的数据进行分组、过滤和筛选。

如下,通过api获取以下格式数组,可以理解为从mysql数据库读取的二维数组:

    var arr = [
        {area_name: "A街道", health_id: "1", health_must: "1693", health_today: "8", checked: "1"},
        {area_name: "B镇", health_id: "2", health_must: "1771", health_today: "2", checked: "1"},
        {area_name: "C镇", health_id: "3", health_must: "1456", health_today: "0", checked: "1"},
        {area_name: "D镇", health_id: "4", health_must: "1458", health_today: "65", checked: "1"},
        {area_name: "E街道", health_id: "5", health_must: "1544", health_today: "2", checked: "1"},
        {area_name: "F街道", health_id: "6", health_must: "1597", health_today: "401", checked: "1"},
        {area_name: "H街道", health_id: "2", health_must: "1481", health_today: "7", checked: "1"},
        {area_name: "I镇", health_id: "6", health_must: "1315", health_today: "2", checked: "1"},
        {area_name: "J镇", health_id: "4", health_must: "1660", health_today: "2", checked: "0"},
        {area_name: "K街道", health_id: "4", health_must: "2240", health_today: "8", checked: "0"},
        {area_name: "L镇", health_id: "3", health_must: "1511", health_today: "8", checked: "0"},
        {area_name: "M街道", health_id: "1", health_must: "1840", health_today: "2", checked: "0"},
        {area_name: "N街道", health_id: "1", health_must: "1738", health_today: "347", checked: "0"},
        {area_name: "P街道", health_id: "1", health_must: "1512", health_today: "2", checked: "0"}
    ];


根据项目需求,我们需要提供以下数据处理结果:

1、获取health_id为指定的数据,如:health_id=1的数据数组;

2、获取多个health_id为指定的数据,如health_id=1,health_id=3,health_id=4……的数据分组数据;

3、获取health_id且(或)checked同时满足的逻辑条件数据,如health_id=1且checked=0;


这些在SQL语句中都有固定的语法,直接调用即可,那么如何在数组中实现以上的需求呢?


解决方案如下:

1.单条件单数据筛选:

    /*1.单条件单数据筛选
     *filte数组过滤,指定对象字段的的筛选方式
     * arr,包含对象的数组;
     * health_id,对象数组对应的键;
     */
    function filterByName(arr, health_id) {
        return arr.filter(item => item.health_id == health_id);
    }
    //获取health_id=4的数组数据;
    var a = filterByName(arr, "4");
    console.log(a);

返回结果:



2.单条件多数据筛选后并自动分组

    /*2.单条件单数据筛选后,并自动分组
     *filte数组过滤,指定对象字段的的筛选方式
     * arr,包含对象的数组;
     * health_id,对象数组对应的键;
     */
    function filterByPolyName(arr, nameArr) {
        var result=[];
        for(var i = 0; i < nameArr.length; i++) {
            result.push(arr.filter(item => item.health_id === nameArr[i]));
        }
        return result;
    }
    //获取health_id=1和3的数组数据;
    var b = filterByPolyName(arr,['1','3']);
    console.log(b);

返回结果:



3、逻辑条件筛选

    /*3.逻辑条件筛选,多条件多数据筛选
     *filte数组过滤,指定对象字段的的筛选方式
     * arr,包含对象的数组;
     * health_id,对象数组对应的键;
     * 根据id且checked筛选;
     */
    function filterByName2(aim, health_id, checked) {
        return aim.filter(item => item.health_id === health_id && item.checked === checked);
    }
    //health_id=1且checked=0筛选
    var c = filterByName2(arr,"1","0");
    console.log(c);

返回结果:



Done!

相关文章
|
5天前
|
安全 Java 大数据
|
19天前
|
SQL API Python
Python DB API下规范下cursor对象常用接口
Python DB API下规范下cursor对象常用接口。
16 4
|
1天前
|
存储 缓存 运维
DataWorks操作报错合集之DataWorks根据api,调用查询文件列表接口报错如何解决
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
9 1
|
2天前
|
SQL 数据管理 API
数据管理DMS产品使用合集之阿里云DMS提供API接口来进行数据导出功能吗
阿里云数据管理DMS提供了全面的数据管理、数据库运维、数据安全、数据迁移与同步等功能,助力企业高效、安全地进行数据库管理和运维工作。以下是DMS产品使用合集的详细介绍。
|
2天前
|
运维 Serverless API
Serverless 应用引擎产品使用之在阿里函数计算中开启函数计算 API 接口如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
101 6
|
5天前
|
前端开发 Java 测试技术
IDEA 版 API 接口神器来了,一键生成文档,贼香!
IDEA 版 API 接口神器来了,一键生成文档,贼香!
17 0
|
6天前
|
API 开发者
邮件API接口使用的方法和步骤
AOKSEND指南:了解和使用邮件API接口,包括选择适合的接口(如AOKSEND、Mailgun、SMTP),获取访问权限,配置发件人、收件人及邮件内容,调用接口发送邮件,并处理返回结果,以高效集成邮件功能。
|
7天前
|
SQL 关系型数据库 API
从API获取数据并将其插入到PostgreSQL数据库:步骤解析
使用Python处理从API获取的数据并插入到PostgreSQL数据库:安装`psycopg2`,建立数据库连接,确保DataFrame与表结构匹配,然后使用`to_sql`方法将数据插入到已存在的表中。注意数据准备、权限设置、性能优化和安全处理。
|
8天前
|
JSON JavaScript API
访问REST API:在Vue中消费和管理远程数据
【4月更文挑战第23天】本文探讨了在Vue应用中高效访问REST API的方法,包括选择合适的API、使用Axios或Fetch发送请求、封装API服务、处理响应和数据、错误管理及性能优化。关键点在于创建服务层封装请求,使用计算属性和方法处理数据,以及实施错误处理和性能提升策略。通过这些最佳实践,开发者能更好地管理和消费远程数据,构建出动态、响应式的Vue应用。
|
8天前
|
Java API Android开发
[NDK/JNI系列04] JNI接口方法表、基础API与异常API
[NDK/JNI系列04] JNI接口方法表、基础API与异常API
12 0