Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: For...Each / With [duplicate]

Tags:

javascript

Possible Duplicate:
JavaScript foreach Vs for

What's the difference between a for loop and for...in? I mean, if there is a difference, it can't be much.

And, I see within validation scripts sometimes the function written like:

function check() {
    with(something) {
        if(){
            // do something
        }
    }
}

What's the point of the "with" condition?

like image 313
dcolumbus Avatar asked Oct 23 '10 06:10

dcolumbus


People also ask

How do you check if there are duplicates in a list JavaScript?

To check if an array contains duplicates: Use the Array. some() method to iterate over the array. Check if the index of the first occurrence of the current value is NOT equal to the index of its last occurrence. If the condition is met, then the array contains duplicates.

Can a JavaScript set have duplicates?

a set is an abstract data type that can store unique values, without any particular order. Let's look at that definition and draw some conclusions: “store unique values” → Sets don't contain duplicates.

How do you filter duplicates in JavaScript?

Use the filter() method: The filter() method creates a new array of elements that pass the condition we provide. It will include only those elements for which true is returned. We can remove duplicate values from the array by simply adjusting our condition.

Is there for each loop in JavaScript?

The forEach method is also used to loop through arrays, but it uses a function differently than the classic "for loop". The forEach method passes a callback function for each element of an array together with the following parameters: Current Value (required) - The value of the current array element.


2 Answers

The for each..in statement iterates a specified variable over all values of an object's properties. For each distinct property, a specified statement is executed. This was introduced by Mozilla in JavaScript 1.6 (see comment by @CMS below), and is not supported in all mainstream browsers.

For each example:

var sum = 0;
var obj = {prop1: 5, prop2: 13, prop3: 8};
for each (var item in obj) {
  sum += item;
}
console.log(sum); // prints "26", which is 5 + 13 + 8

A similar statement is for..in, which iterates over the property names instead of property values. The same example written using for..in:

var sum = 0;
var obj = {prop1: 5, prop2: 13, prop3: 8};
for (var prop in obj) {
  sum += obj[prop];
}
console.log(sum); // prints "26", which is 5 + 13 + 8

The for..in statement has been around since JavaScript 1.0, so you're safe to use it in all browsers.

These statements are different from the traditional for loop, since they are used to iterate over the properties of an object. A for loop can be used to iterate over the elements of an array, but it cannot be used to iterate over the properties of an object, unless you can use ECMAScript 5's Object.keys, which could be used to implement the above example as follows:

var sum = 0;
var obj = {prop1: 5, prop2: 13, prop3: 8};
var keys = Object.keys(obj)
for (var i = 0; i < keys.length; i++) {
  sum += obj[keys[i]];
}
console.log(sum); // prints "26", which is 5 + 13 + 8

As for the with statement, note the following:

JavaScript looks up an unqualified name by searching a scope chain associated with the execution context of the script or function containing that unqualified name. The 'with' statement adds the given object to the head of this scope chain during the evaluation of its statement body. If an unqualified name used in the body matches a property in the scope chain, then the name is bound to the property and the object containing the property. Otherwise a 'ReferenceError' is thrown.

Therefore, consider the following:

var prop1 = 10;
var obj = {prop1: 5, prop2: 13, prop3: 8};

with (obj) {
   console.log(prop1); // prints 5 instead of 10
}

Using with is not recommended, and is forbidden in ECMAScript 5 strict mode. The recommended alternative is to assign the object whose properties you want to access to a temporary variable.

like image 53
Daniel Vassallo Avatar answered Oct 27 '22 22:10

Daniel Vassallo


for each (var i in obj) iterates over the values of an object while for(var i in obj) iterates over the properties. Use it for objects only!

You need a for...in... loop to iterate over properties of an object. A normal for loop would not help you here.


The with statement pushes the properties of the provided object at the beginning of the scope chain. In your example, if something has a property foo, then you can access this property inside the with body just with foo (instead of something.foo).
But note that all other variables, like local variables are now farther down the scope chain, which makes accessing them potentially slower.

Most books and experts recommend to not use with.

like image 25
Felix Kling Avatar answered Oct 27 '22 20:10

Felix Kling