Js操作Array数组
之前写过一篇文章,但是很模糊,不过却给我提供了一个思路,所以没有删除,这次写的是一个完善版本!
因为在很多的时候我们在选中了几行数据,然后存放在Array中,如下图:

看下控制台的数据

我双击了这两个项目

然后我们看看控制台的数据:

数量已经加1了,代码如下:
var _historyData = new Array();
function BindGoodsTab(goodsId, goodsName, goodsPrice) {
var _html = "";
var _data = {};
_data["id"] = parseInt(goodsId);
_data["GoodsName"] = goodsName;
_data["GoodsNum"] = 1;
_data["GoodsPrice"] = parseFloat(goodsPrice);
var _len = _historyData.length; var _Ishave = false;
var _flag = 0;
if (_historyData.length > 0) {
for (var i = 0; i < _len; i++) {
var _hGid = parseInt(_historyData[i]["id"]);
if (_data["id"] == _hGid) {
_flag = i;
_Ishave = true;
break;
}
}
console.log(_flag);
if (!_Ishave) {
_historyData.push(_data);
}
else {
_historyData[_flag]["GoodsNum"] = parseInt(_historyData[_flag]["GoodsNum"]) + 1;
}
}
else {
_historyData.push(_data);
}
console.log(_historyData);
$("#goodsList").html(_html);
}
删除某一个对象:如下图

控制台:

代码:
function DelLease(goodsId) {
for (var i = 0; i < _historyData.length; i++) {
if (goodsId == _historyData[i]["id"]) {
_historyData.splice(i, 1);
}
}
CreateLeaseTab(_historyData);
}
function CreateLeaseTab(_historyData) {
var _html = "";
var _totalMoney = 0;
for (var i = 0; i < _historyData.length; i++) {
_html += "<tr style='border-top:1px solid #ccc;'>";
_html += "<td style='width:160px;padding:7px 0px;'>" + _historyData[i]["GoodsName"] + "</td>";
_html += "<td style='width:60px;'>" + _historyData[i]["GoodsNum"] + "</td>";
_html += "<td style='width:100px;'>" + _historyData[i]["GoodsPrice"].toFixed(2) + "</td>";
_html += "<td style='width:100px;'>" + _historyData[i]["GoodsTotalPrice"].toFixed(2) + "</td>";
_html += "<td style='width:100px;'><img src='../images/Gift/del.png' onclick='DelLease(" + _historyData[i]["id"] + ")' /></td>";
_totalMoney += parseFloat(_historyData[i]["GoodsTotalPrice"].toFixed(2));
}
$("#goodsList").html(_html);
$("#totalMoney").html(_totalMoney.toFixed(2));
console.log(_historyData);
}
Js操作Array数组的更多相关文章
- JS中Array数组的三大属性用法
原文:JS中Array数组的三大属性用法 Array数组主要有3大属性,它们分别是length属性.prototype属性和constructor属性. JS操作Array数组的方法及属性 本文总结了 ...
- 多动手试试,其实List类型的变量在页面上取到的值可以直接赋值给一个js的Array数组变量
多动手试试,其实List类型的变量在页面上取到的值可以直接赋值给一个js的Array数组变量,并且数组变量可以直接取到每一个元素var array1 = '<%=yearList =>'; ...
- js中Array数组基本方法
总结:push() 添加元素到数组未尾, 返回数组长度 unshift() 添加元素到数组头部, 返回数组长度 pop() 删除数组未尾元素, 返回删除元素 shift() 删除数组头部元素, 返回删 ...
- js中Array数组的属性和方法
这是我自己整理出来的一些关于Array数组的属性和方法,即查即用. 1.Array.length属性:数组的项数组,始终返回0或者更大的值. 2.instanceof操作符:value instanc ...
- JS对象 Array 数组对象 数组对象是一个对象的集合,里边的对象可以是不同类型的。数组的每一个成员对象都有一个“下标”,用来表示它在数组中的位置,是从零开始的
Array 数组对象 数组对象是一个对象的集合,里边的对象可以是不同类型的.数组的每一个成员对象都有一个"下标",用来表示它在数组中的位置,是从零开始的 数组定义的方法: 1. 定 ...
- JS判断Array数组中是否包含指定元素
1.调用方式: var arr=["a","b"]; alert(arr.in_array("a")) 2.JS判断数组是否包含指定元素方法 ...
- Js中Array数组学习总结
第一次写博客...有点方... 小白一枚(是真的小白),自学前端,下面来说说我在学习过程中总结的一些数组操作,如果说哪有错误,请各位大神多多指出,小的虚心接受. 引用类型分为Object类型(所谓的对 ...
- js清空array数组的方法
方式1,splice Js代码 ,,,}; ary.length = ; Java中会报错,编译通不过. 而JS中则可以,且将数组清空了,如 Js代码 var ary = [1,2,3,4]; ...
- js删除Array数组中的某个元素
Array.prototype.indexOf = function (val) { ; i < this.length; i++) { if (this[i] == val) return i ...
随机推荐
- C# Math.Round实现中国式四舍五入
decimal sum = 11111.334; sum = , MidpointRounding.AwayFromZero); sum:11111.33decimal sum = 11111.34 ...
- 安装软件出现缺少vcruntime140.dll
安装VC运行库QQ群:616945527 ->VC目录下
- luogu 4047 部落划分 二分答案
二分距离判断是否满足k个部落,注意double类型精度,可使用不开方,最终再开 #include<bits/stdc++.h> #define rep(i,x,y) for(registe ...
- html页面设置<span>的高度和宽度
<span>标签属于行内元素(inline),所以无法设置高度和宽度:如果需要改变其宽高,就需要将其转变为块体元素(block)或行内块体元素(inle-block): 1 span{di ...
- python之实现循环查看指定路径下的所有文件---os.walk
循环查看指定路径下的所有文件.文件夹,包含隐藏文件注:“.filename” 以点开头的是隐藏文件 import os for cur_path,cur_dirs,cur_files in os.wa ...
- LOJ#2249 Luogu P2305「NOI2014」购票
几乎肝了半个下午和整个晚上 斜率优化的模型好多啊... LOJ #2249 Luogu P2305 题意 给定一棵树,第$ i$个点如果离某个祖先$ x$的距离不超过$ L_i$,可以花费$ P_i· ...
- java导出csv文件使用Excel打开乱码问题
写一个csv文件,发现使用 notpad++ 打开是没有问题的,但是使用 Excel 打开之后显示乱码 刚开始的代码是这样子的: ByteArrayOutputStream os = new Byte ...
- Mybatis(二)入门程序-通过id查找用户、模糊查找用户、添加用户、删除用户
根据下图myBatis的架构,创建一个使用MyBatis的工程. 一.配置MyBatis 环境(如图) 1.sqlMapConfig.xml 首先,导入jar包(上图右边)并加载路径,然后 ...
- DeepLearning.ai-Week1-Convolution+model+-+Application
1.0 - TensorFlow model 导入相关依赖包. import numpy as np import h5py import matplotlib.pyplot as plt impor ...
- TensorFlow tf.app&tf.app.flags用法介绍
TensorFlow tf.app&tf.app.flags用法介绍 TensorFlow tf.app argparse tf.app.flags 下面介绍 tf.app.flags.FL ...