js对象数组新增、修改时的验证是否重复的逻辑

摘要:
==person1.id)//如果找到相同对象,则不允许添加if{console.logconsole.log}else{//通过验证,允许新增persons.pushconsole.log}console.logconsole.log('')//---------设定场景2-˃新增时不存在相同姓名---------console.log//新增状态id为-1,验证后重新在前端生成一个id,或者由后台生成person1={id:-1,name:'马六'}//验证verifyObj1=persons.find(item=˃item.name===person1.name&&item.id!

JS代码:

//定义数据集合
const persons =[
  { id: 1, name: '张三'},
  { id: 2, name: '李四'}
]
console.log('')
console.log('初始化数据为:', JSON.stringify(persons))
console.log('')

//--------- 设定场景1 -> 新增时存在相同姓名 ---------
console.log('-------------------场景1-------------------')
//新增状态id为-1,验证后重新在前端生成一个id,或者由后台生成
let person1 = { id: -1, name: '张三'}
//验证
let verifyObj1 = persons.find(item => item.name === person1.name &&item.id !==person1.id)
//如果找到相同对象,则不允许添加
if(verifyObj1) {
  console.log('存在相同姓名,新增失败')
  console.log('场景1结果:', JSON.stringify(persons))
} else{
  //通过验证,允许新增
persons.push(person1)
  console.log('场景1结果:', JSON.stringify(persons))
}
console.log('-------------------场景1-------------------')
console.log('')

//--------- 设定场景2 -> 新增时不存在相同姓名 ---------
console.log('-------------------场景2-------------------')
//新增状态id为-1,验证后重新在前端生成一个id,或者由后台生成
person1 = { id: -1, name: '马六'}
//验证
verifyObj1 = persons.find(item => item.name === person1.name &&item.id !==person1.id)
//如果找到相同对象,则不允许添加
if(verifyObj1) {
  console.log('存在相同姓名,新增失败')
  console.log('场景2结果:', JSON.stringify(persons))
} else{
  person1.id = persons.length + 1 //ID自增
  //通过验证,允许新增
persons.push(person1)
  console.log('场景2结果:', JSON.stringify(persons))
}
console.log('-------------------场景2-------------------')
console.log('')

//--------- 设定场景3 -> 编辑时存在相同姓名 ---------
console.log('-------------------场景3-------------------')
//编辑状态id为实际的编辑对象id
let personIndex = 0 //假设编辑张三
let person2 =persons[personIndex]
person2.name = '李四'
//验证
let verifyObj2 = persons.find(item => item.name === person2.name &&item.id !==person2.id)
//如果找到相同对象,则不允许修改
if(verifyObj2) {
  console.log('存在相同姓名,修改失败')
  console.log('场景3结果:', JSON.stringify(persons))
} else{
  //通过验证,允许修改
  persons[personIndex] =person2
  console.log('场景3结果:', JSON.stringify(persons))
}
console.log('-------------------场景3-------------------')
console.log('')

//--------- 设定场景4 -> 编辑时不存在相同姓名 ---------
console.log('-------------------场景4-------------------')
//编辑状态id为实际的编辑对象id
personIndex = 0 //假设编辑张三
person2 =persons[personIndex]
person2.name = '王五'
//验证
verifyObj2 = persons.find(item => item.name === person2.name &&item.id !==person2.id)
//如果找到相同对象,则不允许修改
if(verifyObj2) {
  console.log('存在相同姓名,修改失败')
  console.log('场景4结果:', JSON.stringify(persons))
} else{
  //通过验证,允许修改
  persons[personIndex] =person2
  console.log('场景4结果:', JSON.stringify(persons))
}
console.log('-------------------场景4-------------------')
console.log('')

运行结果:

js对象数组新增、修改时的验证是否重复的逻辑第1张

免责声明:文章转载自《js对象数组新增、修改时的验证是否重复的逻辑》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Java数组与vector互转关于跨域的SessionID的问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

Pycharm Debug功能详解

左键单击代码编辑区域中的行号以在调试模式下运行代码:单击左上角工具栏上的调试图标(bug图)。方法2:在调试模式下运行后,1。代码编辑区域中的蓝色条是当前程序运行的位置,即c=add(a)应该运行,但尚未运行。2.代码编辑区域中的深红色条是当前程序设置的所有断点行。3.左下方是程序堆栈,进入fun()函数。4.右下方是可变显示区域1。跳转到当前断点(在断点之...

微信小程序生成带参数的二维码(小程序码)独家asp.net的服务端c#完整代码

1) 我第一次使用wx。小程序端请求调用API,发现这是一个坑!@-_~Page:'pages/index/index',//在此处填写要跳转到的小程序页面。你不能在它前面添加/oh。发布后必须为1024页//小程序代码的边长,以像素为单位,范围[2801280]},标头:{'content-type':“application/json;charset=U...

记一次Arcgis Server10.2许可过期导致发布图层失败

1.今天,当使用arcmap将地图服务发布到arcgisserver时,发布突然失败。在arcgisserver的管理页面的日志选项中发现错误:未能初始化服务器对象“System/PublicingTools”:0x80004005:错误:(-8003)YourArcGISServerlicense已过期。2.然后在服务器路径中查找文件:...

SpringBoot源码深度解析

Spring开源框架解决了企业开发的复杂性,简化了AOP的开发,IOCSpring配置越来越多,不易管理==如何自动配置Springboot,核心原则!Java领域最流行的技术!公司,如何演变结构!...

Winform知识点

BringToFront()将控件移动到Z顺序的前面。...

go语言游戏服务端开发(一)——架构

本教程以Go语言为例。特别是游戏服务进程有更新上线时,稳定性还没有被线上并发验证,宕机的几率会增加,数据丢失的风险也会增加。为了减轻风险,可以考虑把数据缓存跟服务进程分离。对于轻中度游戏,游戏的通信量不会很多,没必要每个分服都有一个长连接socket网关。假设一个分服同时连接服务器的客户端有5k,一台机器的socket网关能支持5w个玩家。因此网关需要参与服...