简体   繁体   English

Lodash forEach Associative Array

[英]Lodash forEach Associative Array

Is there a forEach loop in Lodash for associative arrays? Lodash是否存在用于关联数组的forEach循环? The function called "forEach", I've found, only works for indexed arrays. 我发现, 名为 “forEach”的函数仅适用于索引数组。 For example, if I have an array myArray with values [1, 2, 3] , and do 例如,如果我有一个数组myArray ,其值为[1, 2, 3]和do

lodash.forEach(myArray, function(index) {
    console.log(index);
});

and run the function (in Node ), I get the expected result: 并运行该函数(在Node ),我得到了预期的结果:

1
2
3

However, when I try this with an associative array, it doesn't work: 但是,当我尝试使用关联数组时,它不起作用:

lodash = require('lodash');
myArray = [];
myArray['valOne'] = 1;
myArray['valTwo'] = 2;
myArray['valThree'] = 3;
lodash.forEach(myArray, function(index) {
    console.log('7');
});

As you can see from running this in Node , the callback function doesn't fire even when it includes something other than the array elements. 正如您在Node运行它所看到的那样,即使回调函数包含除数组元素之外的其他内容,它也不会触发。 It just seems to skip the loop entirely. 它似乎完全跳过循环。

First of all, why does this happen? 首先,为什么会发生这种情况? Second of all, is there another function included in Lodash for this problem, or, if not, is there a way to use the forEach function to accomplish this, without changing the original array in the process? 第二, Lodash是否有另一个函数用于此问题,或者,如果没有,是否有办法使用forEach函数来完成此任务,而无需更改进程中的原始数组?

myArray = [];
myArray['valOne'] = 1;
myArray['valTwo'] = 2;
myArray['valThree'] = 3;
lodash.forEach(myArray, function(index) {
    console.log('7');
});

An associative array is just a set of key value pairs, which is nothing but a Javascript object. 关联数组只是一组键值对,它只是一个Javascript对象。 Above case - myArray.length === 0 , You are just addding properties to the array object, not adding any values to actual array. 以上情况 - myArray.length === 0 ,您只是向数组对象添加属性,而不是向实际数组添加任何值。

Instead initialize your myArray like this and loop through using forIn 而是像这样初始化你的myArray并使用forIn循环

var myArray = {};
myArray['valOne'] = 1;
myArray['valTwo'] = 2;
myArray['valThree'] = 3;

lodash.forIn(myArray, function(value, key) {
    console.log(key + " : " + value); 
});

OR just 要不就

   var  myArray = {
    valOne  : 1,
    valTwo  : 2,
    valThree : 3
   };

   lodash.forIn(myArray, function(value, key) {
        console.log(key + " : " + value); 
    });

More about Object as Associative Array here 这里有关于Object as Associative Array的更多信息

Lodash has the function forOwn for this purpose. Lodash具有的功能forOwn用于这一目的。 In the second array, if you do 在第二个数组中,如果你这样做

_.forOwn(myArray, function(index) {
    console.log(index);
});

you should get the intended result. 你应该得到预期的结果。

I'm still not sure why forEach seems to skip the first function, however, but I believe it may have to do with the array not having a "length". 我仍然不确定为什么forEach似乎跳过了第一个函数,但我相信它可能与没有“长度”的数组有关。 A JavaScript array's length is the highest numbered index it has. JavaScript数组的长度是它具有的最高编号索引。 For example, an array myOtherArray defined as myOtherArray[999]="myValue" will have a length of 1,000 (because arrays are zero-indexed, meaning they start at 0, not 1), even if it has no other values. 例如,定义为myOtherArray[999]="myValue"的数组myOtherArray将具有1,000的长度(因为数组是零索引的,意味着它们从0开始,而不是1),即使它没有其他值。 This means an array with no numbered indexes, or only negative indexes, will not have a length attribute. 这意味着没有编号索引的数组或只有负索引的数组将不具有length属性。 Lodash must be picking up on this and not giving the array a length attribute, likely to maintain consistency and performance, thus not rendering any output. Lodash必须接受这个并且不给数组一个length属性,可能保持一致性和性能,因此不会呈现任何输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM