I normally use while loop as:
while (i<some_value)
I saw while(i--) syntax and thought it is shorter and cooler and tried the following in google-chrome.
var num_arr= [4,8,7,1,3];
var length_of_num_arr=num_arr.length;
while(length_of_num_arr--) console.log(num_arr);
[4, 8, 7, 1, 3]
[4, 8, 7, 1, 3]
[4, 8, 7, 1, 3]
[4, 8, 7, 1, 3]
[4, 8, 7, 1, 3] **// THIS IS EXPECTED RESULT**
But When I try...
while((num_arr.length)--) console.log(num_arr);
[4, 8, 7, 1]
[4, 8, 7]
[4, 8]
[4]
[] // WHY IS THIS HAPPENING??
Is there some hidden things you need to understand to use this syntax?
Arrays’ length property is writable, and will cut off their elements or add empty slots as appropriate when you set it.
var items = [1, 2, 3, 4, 5];
items.length = 3; // items is now [1, 2, 3]
items.length = 6; // items is now a sparse array: [1, 2, 3, undefined × 3]
So, don’t do that.
When you do array.length-- you're potentially shortening the array by one element each time.
See the reference under the section Shortening the array from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length
Array.prototype.length can be re-written programmatically and it potentially shorten your array by the new length you assign.
For example
a = [1,2,3,4,5,6,7,8,9,10];
// Shorten the array by one element
a.length--; // a <-- [1,2,3,4,5,6,7,8,9]
// In case you want to shorten your array to 3 elements, you can:
a.length = 3; // a <-- [1,2,3]
When you set the length property of an array to a lower value, the items at the end are removed:
var arr = [1,2,3,4,5];
arr.length; // 5
arr.length = 3;
arr; // [1,2,3]
This is described in the spec:
While newLen < oldLen repeat,
Set oldLen to oldLen – 1.
Let deleteSucceeded be the result of calling the [[Delete]] internal method of A passing ToString(oldLen) and false
as arguments.
In your code you use the postfix decrement operator (--) which reduces the length of the array.
Related
Hello I'm new to javascript.
I was trying to reduce the length of an array by 1.
I did this, the last element is undefined now, but the length is still 5.
var arr = [1, 2, 3, 4, 5];
arr.length = arr.length--;
console.log(arr);
console.log(arr.length);
I'm confused. Instead of [1, 2, 3, 4] or [1, 2, 3, 4, 5]. Why it gives me [1, 2, 3, 4, undefined]?
Can someone explain? Thanks
arr.length-- will reduce the length by 1 already but will return the previous length, meaning you're reducing the array's length by 1 and then immediately assigning the previous length to arr.length, meaning you're changing it back to a five element array (but you already removed the fifth element ,so that's now undefined.
If you want to just reduce the array's length, you can user arr.length--; alone.
var arr = [1, 2, 3, 4, 5];
arr.length--;
console.log(arr);
console.log(arr.length);
You need to use array.prototype.pop you instead to reduce the size of your array:
var arr = [1, 2, 3, 4, 5];
arr.pop();
console.log(arr);
console.log(arr.length);
Take into consideration that arr.length-- with a length of 0 causes an error.
A timeline of what's happening in array.length = array.length--:
The -- operator changes array.length from 5 to 4.
array.length-- evaluates to the previous value of array.length, which is 5
This value is assigned to array.length, changing it from 4 to 5.
So you are shrinking the array and then growing it.
My advice: avoid mutating your arrays and instead use slice:
var arr = [1, 2, 3, 4, 5];
var newArr = arr.slice(0, -1);
console.log(newArr);
console.log(newArr.length);
If you insist on mutating your arrays, then I suggest you use a less mystifying operator: -=:
var arr = [1, 2, 3, 4, 5];
arr.length -= 1;
console.log(arr);
console.log(arr.length);
var arr = [1, 2, 3, 4, 5];
arr.splice(arr.length-1);
console.log(arr); //[1,2,3,4] --Expected Log
console.log(arr.length); //4 --Expected Log
Or you can try this:
var arr = [1, 2, 3, 4, 5];
arr.pop();
console.log(arr); //[1,2,3,4] --Expected Log
console.log(arr.length); //4 --Expected Log
if you want reduce , you can use 'splice'
Here's a sample of the problem I'm having in JavaScript:
first array [1, 2, 3, 4, 5, 6, 7]
second array [7, 8, 9, 4, 2, 5, 7]
In this case, I need to be able to find and eliminate "4" and "7" from both arrays, eliminating both. This is based on their location and matching value.
I haven't been able to find anything other than eliminating matching values. In this case, however, the values must be in the same place and also be matching.
I've tried this so far:
function findCommonElements3(array1, array2) {
return arr1.some(item => arr2.includes(item))
}
it looks like it only looks for matching elements, whereas I need to find matching corresponding elements and then remove them.
As mentioned in the comments, you may use the splice method to remove one or more elements of an array in JavaScript.
First of all I would store the indexes of the elements I should remove looping the array as so:
const array1 = [1, 2, 3, 4, 5, 6, 7];
const array2 = [7, 8, 9, 4, 2, 5, 7];
//Indexes of same elements
var sameIndexes = [];
function findSameIndexes(element, index) {
if (array1[index] == array2[index]) {
sameIndexes.push(index);
}
}
array1.forEach(findSameIndexes);
Calling console.log(sameIndexes) should give this result:
Array [3, 6]
The problem is that if you loop again the array and remove the elements in that order, the indexes would not correspond to the elements anymore.
For example if you remove the 3rd element, the number 7 wouldn't be at index 6 anymore, to solve this issue I'd use the reverse method so you won't lose track of the indexes
// A simple function to remove the elements in both arrays
function removeElements(index) {
array1.splice(index,1);
array2.splice(index,1);
}
sameIndexes.reverse().forEach(removeElements);
And the final results would be
Array [1, 2, 3, 5, 6]
Array [7, 8, 9, 2, 5]
Which hopefully is what you were looking for, of course there are better ways to write it down, but maybe this will help you find a solution.
You could just use a for loop and use index. something like this
const firstarray = [1, 2, 3, 4, 5, 6, 7]
const secondarray = [7, 8, 9, 4, 2, 5, 7]
for (let i = 0; i <= firstarray.length - 1; i++) {
if (firstarray[i] === secondarray[i]) {
console.log(`found ${firstarray[i]} at index ${i}`);
firstarray.splice(i, 1);
secondarray.splice(i, 1);
}
}
console.log(firstarray, secondarray);
const excludeCommon = (ar1, ar2) => {
const both = [...ar1, ...ar2].filter((v, i, ar) => v !== ar[i + (2 * (i < ar1.length) - 1) * ar1.length]);
return [both.slice(0, both.length / 2), both.slice(both.length / 2)];
}
console.log(excludeCommon([1, 2, 3, 4, 5, 6, 7], [7, 8, 9, 4, 2, 5, 7]));
I'm trying to swap the two lowest values in a shuffled array containing the numbers 0-14. For those curious, I'm implementing the shuffling algorithm for a 15 puzzle described by pkpnd here.
I wanted to try destructuring assignment, as described here, but am encountering an unexpected behavior. I realize that I can get my code working (and make it more readable) by just creating a temporary variable, but I'd like to understand what's happening before moving on.
I'm grabbing a subset of my array [1,2] and then trying to replace it with [2,1]. For some reason, it's only swapping the values when their order in the original array is opposite of the order of my subset.
I originally tried this:
var arr1 = [1, 2, 3, 4];
var arr2 = [2, 1, 3, 4];
[arr1[arr1.indexOf(1)], arr1[arr1.indexOf(2)]] = [arr1[arr1.indexOf(2)], arr1[arr1.indexOf(1)]];
[arr2[arr2.indexOf(1)], arr2[arr2.indexOf(2)]] = [arr2[arr2.indexOf(2)], arr2[arr2.indexOf(1)]];
console.log("arr1: " + arr1, "\narr2: " + arr2);
And then tried this:
var arr1 = [1, 2, 3, 4];
var arr2 = [2, 1, 3, 4];
[arr1[arr1.indexOf(1)], arr1[arr1.indexOf(2)]] = [2, 1];
[arr2[arr2.indexOf(1)], arr2[arr2.indexOf(2)]] = [2, 1];
console.log("arr1: " + arr1, "\narr2: " + arr2);
But both produce identical output:
arr1: 1,2,3,4
arr2: 1,2,3,4
I would expect that the position of 1 and 2 would be swapped for both arrays, but they're only swapped in arr2. I suspect this has to do with they way the initial array subset [1,2] is created, but I'm not sure.
Can anybody explain why the values aren't always swapped?
The result is simple, because it goes step for step for each item of destructuring assingment. And while the values are changing, the index of the values changes.
Case 1 [1, 2, 3, 4]
Get index of target for the first value 2
[arr1[arr1.indexOf(1)], arr1[arr1.indexOf(2)]] = [2, 1];
// ^^^^^^^^^^^^^^^ 0
Assign 2 to arr1[0]
[2, 2, 3, 4]
Get index of target for the second value 1:
[arr1[arr1.indexOf(1)], arr1[arr1.indexOf(2)]] = [2, 1];
// ^^^^^^^^^^^^^^^ 0
Assign 1 to arr1[0]
[1, 2, 3, 4]
Case 2 [2, 1, 3, 4]
Get index of target for the first value 2
[arr2[arr2.indexOf(1)], arr2[arr2.indexOf(2)]] = [2, 1];
// ^^^^^^^^^^^^^^^ 1
Assign 2 to arr1[1]
[2, 2, 3, 4]
Get index of target for the second value 1:
[arr2[arr2.indexOf(1)], arr2[arr2.indexOf(2)]] = [2, 1];
// ^^^^^^^^^^^^^^^ 0
Assign 1 to arr1[0]
[1, 2, 3, 4]
i am trying to splice the odd/evens numbers, look this, i tried to find odd numbers , but on the results array, evens numbers still there ,
function find(index){
for(var i = 0 ; i < index.length;i++) {
for(var j = 0 ; j < index[i].length ; j++) {
if(index[i][j] % 2 === 1) { // trying to find odd numbers
index[i].splice(j, 1)
}
}
}
return index
}
var data = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
var another = [
[2, 2, 7],
[1, 3, 5, 6],
[1, 3, 5, 7, 9, 11]
]
console.log(find(data))
console.log(find(another))
is there any wrong with my codes ?? or i missed something? :)
The problem is that you are mutating the array as you loop over it.
ie. on the second row of your second array, ([1,3,5,6]), lets think about what happens:
i =1, j = 0
the number (1) is odd, so you splice it, the array now looks like [3,5,9]
i = 1, j = 1, the number is 5, is odd, so you remove it. You skipped over 3.
i = 1, j=2, length of the row is 2, so the loop ends.
I've added a console log to your code that demonstrates this.
function find(index){
for(var i = 0 ; i < index.length;i++) {
for(var j = 0 ; j < index[i].length ; j++) {
if (i ===1) console.log(`i = ${i}, j=${j}, index[i] = ${index[i]}, index[i].length=${index[i].length}`);
if(index[i][j] % 2 === 1) { // trying to find odd numbers
index[i].splice(j, 1)
}
}
}
return index
}
var another = [
[2, 2, 7],
[1, 3, 5, 6],
[1, 3, 5, 7, 9, 11]
]
console.log(find(another))
So generally, as a rule, don't loop over arrays and mutate them.
Instead, I recommend using the Array.prototype methods where you can, instead of loops.
A CertainPerformance suggests - you could use filter here instead.
function find(array){
//Assuming array is a 2d array.
return array.map(row => row.filter(v => v%2 ===0));
}
var data = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
var another = [
[2, 2, 7],
[1, 3, 5, 6],
[1, 3, 5, 7, 9, 11]
]
console.log(find(data))
console.log(find(another))
Note that this does return some empty arrays, which maybe you want to keep, or maybe you want to filter again to remove the empty arrays.
Note that, if you're not familiar with it - I'm using the ES6 fat arrow syntax in my answer.
I need to check if all items in an array can be found within another array. That is, I need to check if one array is a subset of another array.
Example:
var array = [1, 2, 5, 7];
var otherArray = [1, 2, 3, 4, 5, 6, 7, 8];
Comparing these two arrays above should return true as all items in array can be found in otherArray.
var array = [1, 2, 7, 9];
var otherArray = [1, 2, 3, 4, 5, 6, 7, 8];
Comparing these two arrays above should return false as one of the items in array cannot be found in otherArray.
I have tried to use the indexOf method inside a for loop without success.
I hope someone could help me. :)
Use Array.prototype.every:
The every() method tests whether all elements in the array pass the test implemented by the provided function.
var array = [1, 2, 7, 9];
var otherArray = [1, 2, 3, 4, 5, 6, 7, 8];
var isSubset = array.every(function(val) {
return otherArray.indexOf(val) >= 0;
})
document.body.innerHTML = "Is array a subset of otherArray? " + isSubset;