JavaScript中遇到_.has;_.contains;_.where;

本文介绍了Underscore.js库中的三个核心函数:has、contains(alias includes)和where的功能及用法。has用于检查对象是否包含指定键;contains用于判断列表是否包含特定值;where则用于从列表中筛选出符合特定属性的对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这三个函数是Underscore的中一些函数;Underscore是一个JavaScript实用库,提供了一整套函数式编程的实用功能(这里不详细介绍,百度都可以搜到)。

has_.has(object, key) 
对象是否包含给定的键吗?等同于object.hasOwnProperty(key),但是使用hasOwnProperty 函数的一个安全引用,以防意外覆盖

_.has({a: 1, b: 2, c: 3}, "b");
=> true

contains_.contains(list, value, [fromIndex]) Alias: includes 
如果list包含指定的value则返回true(愚人码头注:使用===检测)。如果list 是数组,内部使用indexOf判断。使用fromIndex来给定开始检索的索引位置。

_.contains([1, 2, 3], 3);
=> true

where_.where(list, properties) 
遍历list中的每一个值,返回一个数组,这个数组包含properties所列出的属性的所有的 键 - 值对。

_.where(listOfPlays, {author: "Shakespeare", year: 1611});
=> [{title: "Cymbeline", author: "Shakespeare", year: 1611},
    {title: "The Tempest", author: "Shakespeare", year: 1611}]

using CY_PUBLIC_LIBRARY.Extension; using DoMain.hp_dto; using Model.sys_model; using System.Transactions; namespace CY_API.Controllers.hController { [Route("api/[controller]")] [Authorize] [ApiExplorerSettings(GroupName = "base_data")] public class HospitalController : BaseApiController { public HospitalController(IFreeSql fsql) : base(fsql) { } /// <summary> /// 新增 /// </summary> /// <param name="hospitalDto">医院和科室信息</param> /// <returns></returns> [HttpPost] public IActionResult AddHospitalWithDepartments([FromBody] hospitalDepartmentDto hospitalDto) { try { // 使用事务确保数据一致性 _fsql.Transaction(() => { // 1. 创建医院 var hospital = new sys_hospital { name = hospitalDto.name, address = hospitalDto.address, image = hospitalDto.image, hospital_label = hospitalDto.hospital_label, is_hot = hospitalDto.is_hot, }; // 插入医院并获取自增ID var hospitalId = _fsql.Insert(hospital).ExecuteIdentity(); // 2. 创建科室 var departments = hospitalDto.hospitalDepartmentListDtos.Select(d=> new sys_hospitaldepartment { name = d.name, hospital_id =(int)hospitalId, content = d.content, //hd_id=d.hd_id }).ToList(); // 批量插入科室 _fsql.Insert(departments).ExecuteAffrows(); }); return this.ReturnOk(); } catch (Exception ex) { throw ex; } } /// <summary> /// 显示医院列表 /// </summary> /// <returns></returns> [HttpGet] public ActionResult<retEnumerable<retHospital>> GetHospital(string name, bool? is_hot, int page=1, int size=5) { try { var query = _fsql.Select<sys_hospital>(); if (!string.IsNullOrEmpty(name)) { query = query.Where(x => x.name.Contains(name)); } if (is_hot.HasValue) { query = query.Where(x => x.is_hot == is_hot.Value); } var hospitals =query.OrderByDescending(x => x.hospital_id).Page(page, size).ToList(); // 获取每个医院的科室信息 var result = hospitals.Select(hospital => new retHospital { hospital_id = hospital.hospital_id, name = hospital.name, address = hospital.address, image = hospital.image, hospital_label = hospital.hospital_label, is_hot = hospital.is_hot, // 获取该医院的科室列表 departments = _fsql.Select<sys_hospitaldepartment>().Where(d => d.hospital_id == hospital.hospital_id).Page(page,size) .ToList(d => new retHospitalDepartment { hd_id = d.hd_id, name = d.name, content = d.content, hospital_id = d.hospital_id }) }).ToList(); return Ok(result.GetRetEnumerable()); } catch (Exception ex) { throw ex; } } /// <summary> /// 删除 /// </summary> /// <param name="id"></param> /// <returns></returns> [HttpDelete("{id}")] public IActionResult DeleteHp(int id) { _fsql.Transaction(() => { _fsql.Update<sys_hospital>(id).Set(e => e.is_del, true).ExecuteAffrows(); }); return this.ReturnOk(); } /// <summary>. /// 修改医院和科室 /// </summary> /// <param name="upd">医院和科室信息</param> /// <returns></returns> [HttpPut] public IActionResult UpdateHospitalWithDepartments([FromBody] updatehospitalDepartment upd) { try { // 使用事务确保数据一致性 _fsql.Transaction(() => { // 1. 更新医院信息 var hospital = new sys_hospital { hospital_id = upd.hospital_id, name = upd.name, address = upd.address, image = upd.image, hospital_label = upd.hospital_label, is_hot =upd.is_hot }; _fsql.Update<sys_hospital>().SetSource(hospital).ExecuteAffrows(); // 2. 更新科室信息 var departments = upd.updhospitalDepartmentListDtos.Select(d => new sys_hospitaldepartment { hd_id = d.hd_id, name = d.name, content = d.content, hospital_id = upd.hospital_id, }).ToList(); // 修改关联科室 _fsql.Update<sys_hospitaldepartment>() .Where(d => d.hospital_id == hospital.hospital_id).SetSource(departments) .ExecuteAffrows(); }); // 返回更新后的医院详情(包含科室) return Ok(upd.hospital_id); } catch (Exception ex) { throw ex; } } } } 为啥我后台测试的时候都是对的,但前端调用接口会报错,修改不成功,而且有时候前端也添加少张表添加进去
08-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值