forEach遍历数组对象

var obj1 = [{

key: '01',

value: '哈哈'

}, {

key: '02',

value: '旺旺'

}, {

key: '03',

value: '娃娃'

}, {

key: '04',

value: '皮皮'

}, {

key: '05',

value: '波波'

}];

//  遍历数组对象

var str1 = "";

angular.forEach(obj1, function(data, index, obj1) {

//data等价于obj1[index]

str1 += obj1[index].value + ',';

});

str1 = str1.substring(0, str1.length - 1);

console.log(str1); //  哈哈,旺旺,娃娃,皮皮,波波

//index 数组参数都可以省略

var str2 = "";

angular.forEach(obj1, function(data) {

str2 += data.key + ',';

})

str2 = str2.substring(0, str2.length - 1);

console.log(str2); //  01,02,03,04,05

//forEach() 遍历数组对象

var str3 = "";

obj1.forEach(function(data){

str3 += data.value + ',';

})

str3 = str3.substring(0, str3.length - 1);

console.log(str3); //  哈哈,旺旺,娃娃,皮皮,波波

//  遍历数组对象   根据主键去重

var o = {};

var arr = [];

angular.forEach(obj1, function(data) {

if(!o[data.key]){

arr.push(data.key);

o[data.key] = true;

}

});

console.log(arr); //  ["01", "02", "03", "04"]

语法:

array:需要遍历的集合

data:遍历时当前的数据(数组中的每一项)

index:遍历时当前索引

这里要注意的是:function()里面的参数第一个是value ,第二个是下标(index),第三个是要便利的数组;

也可以不用写后面两个参数

 var array = [{a: 1}, {b: 2}];

 angular.forEach(array , function(data, index, array){

console.log(data == array[index]); //true }) array.forEach(function(data, index, array){ console.log(data == array[index]); //true })

forEach遍历数组对象且去重的更多相关文章

  1. js遍历数组对象和非数组对象

    //---------for用来遍历数组对象 var i,myArr = ["a","b","c"]; ; i < myArr.len ...

  2. JavaScript 数组对象的去重

    JavaScript数组去重 1.原型去重法.通过prototype找到数组的源性对象Array,在数组的原型上添加unique()方法.需要使用的时候使用 点 " . " 进行连 ...

  3. 遍历数组,对象和JSON

    遍历数组 var arr2 = [3,4,5,6,7,8]; //第一种方法 for(var i =0;i<arr.length;i++){ console.log(arr2[i]); } // ...

  4. foreach遍历数组、数组的转置与方阵的迹

    public class Copy1 { public static void main(String[] args) { array1(); //如果不初始化元素,默认为0 int [][] a = ...

  5. 用数组指针遍历数组,FOR/FOREACH遍历数组

    1. 用数组指针遍历一维数组 <?php header("Content-type:text/html;charset=utf-8"); /*用数组指针遍历一位数组的值*/ ...

  6. foreach遍历数组

    foreach遍历一维数组 <?php //PHP数组遍历:foreach //定义数组 $arr=array(1,2,3,4,5,6,7,8,9,10); //foreach循环 foreac ...

  7. PHP foreach遍历数组之如何判断当前值已经是数组的最后一个

    先给出foreach的两种语法格式 1,foreach (array_expression as $value) statement 2,foreach (array_expression as $k ...

  8. forEach遍历数组实例

    forEach()取出数组中2的倍数和3的倍数的数 //for IE if(!Array.prototype.forEach){ Array.prototype.forEach = function( ...

  9. ajax遍历数组对象

    success: function(data){ console.log(data); for (var warn in data) { alert(data[warn].kh_lxr); } } d ...

随机推荐

  1. Windows:打开MSDTC,恢复Windows任务栏,查看windows日志,打开远程桌面,打开Services,资源监控

    Windows 服务器系列: Windows:查看IP地址,IP地址对应的机器名,占用的端口,以及占用该端口的应用程 Windows:使用Dos命令管理服务(Services) Windows:任务调 ...

  2. [Vue warn]:vue-Failed to resolve directive: clipboard

    前言 需求:移动端需要一个按钮,复制到剪切板,分享给好友(没有调用微信内置的分享接口) 插件 vue-clipboard2 环境:vue,node 安装:npm install --save vue- ...

  3. SpringBoot日记——登录与拦截器篇

    之前的文章我们把登录页写了出来,但是想要让登录现实他的基本功能,要如何做呢?本篇文章就来帮你实现第一步,让登录页对账号密码做校验,并且完成登录跳转. LoginController 1. 要实现登录, ...

  4. SecureCRT 用法总结

    SecureCRT 用法总结   1.下载与破解方法: Mac:https://www.jianshu.com/p/9427f12b1fdb Window:https://drive.google.c ...

  5. python爬虫beautifulsoup4系列2

    前言 本篇详细介绍beautifulsoup4的功能,从最基础的开始讲起,让小伙伴们都能入门 一.读取HTML页面 1.先写一个简单的html页面,把以下内容copy出来,保存为html格式文件 &l ...

  6. git pull fatal: refusing to merge unrelated histories

    1.首先我github有个远程仓库,然后我本地有个仓库 本地仓库我新添加了一个文件,然后我去关联(git remote add origin git@github.com:qshilary/gitte ...

  7. 《杜增强讲Unity之Tanks坦克大战》8-子弹碰撞处理

    8 子弹碰撞处理 为了处理子弹打到坦克的伤害我们在这里新建一个Shell.cs 子弹有两种情况,碰到坦克炸开,没有碰到坦克则过2s子弹销毁. void Start () { Destroy (game ...

  8. linux 启动自动运行

    开机启动时自动运行程序  Linux    1.加载后, 它将初始化硬件和设备驱动, 然后运行第一个进程init.init根据配置文件继续引导过程,启动其它进程.通常情况下,修改放置在 /etc/rc ...

  9. Django 前后端不分离 代码结构详解

    Demo:  hello_pycharm 根目录文件:hello_pycharm [__init__.py  __pycache__  settings.py  urls.py  wsgi.py] A ...

  10. Netty源码分析第5章(ByteBuf)---->第5节: directArena分配缓冲区概述

    Netty源码分析第五章: ByteBuf 第五节: directArena分配缓冲区概述 上一小节简单分析了PooledByteBufAllocator中, 线程局部缓存和arean的相关逻辑, 这 ...