JS(JQuery)操作Array的相关方法介绍_javascript技巧
程序员文章站
2022-03-30 10:48:14
...
1:split 函数将字符串按某个字符分割,将分割后的结果存入字符串数组中
function SplitUsersInformation(users) {
var usersArray = users.split(';');
return usersArray;
}
2:substr 函数切割目标字符串
currentStr = currentStr .substr(0, currentStr.length - 2);
3:push 方法向Array中添加一条记录
var totalUsers = new Array();
function PushItem(name, departmemt) {
var currentUser = new Object();
currentUser.UserName = name;
currentUser.Department = departmemt;
totalUsers.push(currentUser);
}
4:pop 方法从Array的栈中弹出最上面的那条记录
var totalUsers = new Array();
var user1 = new Object();
user1.UserName = "haha";
user1.Department = "hahahaha";
var user2 = new Object();
user2.UserName = "lolo";
user2.Department = "lolololo";
totalUsers.push(user1);
totalUsers.push(user2);
totalUsers.pop();
//totalUsers中会剩下user1,因为user2在栈的最上方,被弹出
5:splice 方法从Array中删除指定的某条或者多条记录
var totalUsers = new Array();
totalUsers.push(...);
复制代码 代码如下:
function SplitUsersInformation(users) {
var usersArray = users.split(';');
return usersArray;
}
2:substr 函数切割目标字符串
复制代码 代码如下:
currentStr = currentStr .substr(0, currentStr.length - 2);
3:push 方法向Array中添加一条记录
复制代码 代码如下:
var totalUsers = new Array();
function PushItem(name, departmemt) {
var currentUser = new Object();
currentUser.UserName = name;
currentUser.Department = departmemt;
totalUsers.push(currentUser);
}
4:pop 方法从Array的栈中弹出最上面的那条记录
复制代码 代码如下:
var totalUsers = new Array();
var user1 = new Object();
user1.UserName = "haha";
user1.Department = "hahahaha";
var user2 = new Object();
user2.UserName = "lolo";
user2.Department = "lolololo";
totalUsers.push(user1);
totalUsers.push(user2);
totalUsers.pop();
//totalUsers中会剩下user1,因为user2在栈的最上方,被弹出
5:splice 方法从Array中删除指定的某条或者多条记录
复制代码 代码如下:
var totalUsers = new Array();
totalUsers.push(...);
function SpliceItem(name) {
for (var i = 0; i if (totalUsers[i].UserName == name) {
totalUsers.splice(i, 1)
}
}
}
推荐阅读
-
Js数组的操作push,pop,shift,unshift等方法详细介绍_javascript技巧
-
JS获取并操作iframe中元素的方法_javascript技巧
-
一个js导致的jquery失效问题的解决方法_javascript技巧
-
以JSON形式将JS中Array对象数组传至后台的方法_javascript技巧
-
jquery库文件略庞大用纯js替换jquery的方法_javascript技巧
-
以JSON形式将JS中Array对象数组传至后台的方法_javascript技巧
-
js与jquery实时监听输入框值的oninput与onpropertychange方法_javascript技巧
-
原生JS和JQuery动态添加、删除表格行的方法_javascript技巧
-
JS操作HTML自定义属性的方法_javascript技巧
-
js实现类似jquery里animate动画效果的方法_javascript技巧