Here is my example.
Can you tell me how can I make the array have consecutive keys? I want to reindex my array.
Currently I have:
var testArray = new Array();
testArray[3]="qwerty";
testArray[7]="asdfgh";
testArray[13]="zxcvbn";
console.log(testArray);
But I'd like to get the values at indices 0, 1 and 2 (and so on):
["qwerty", "asdfgh", "zxcvbn"]
The re-index of an array can be done by using some inbuilt function together. These functions are: array_combine() Function: The array_combine() function is an inbuilt function in PHP which is used to combine two arrays and create a new array by using one array for keys and another array for values.
To change the position of an element in an array:Use the splice() method to insert the element at the new index in the array. The splice method changes the original array by removing or replacing existing elements, or adding new elements at a specific index.
Array.prototype.filter()
is not executed on deleted or previously undefined items. So you can simply do:
testArray.filter(function(val){return val});
..in order to re-index your array.
Or ES6:
testArray.filter(val => val)
If you don't mind using javascript 1.6: (note: this code uses the jQUery library)
var testArray = new Array();
testArray[3]="qwerty";
testArray[7]="asdfgh";
testArray[13]="zxcvbn";
var testString = testArray.filter(function (item) { return item != undefined }).join();
$(function(){
$('#write').text(testString);
});
filter prototype:
if (!Array.prototype.filter)
{
Array.prototype.filter = function(fun /*, thisp */)
{
"use strict";
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();
var res = [];
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in t)
{
var val = t[i]; // in case fun mutates this
if (fun.call(thisp, val, i, t))
res.push(val);
}
}
return res;
};
}
You could filter the array by using a callback which returns true
or other truthy value, because Array#filter
omits sparse elements.
If Boolean
is taken, the result filters not only sparse items, but items which have a falsy value. To prevent this, take a callback which returns true
for every element.
array.filter(_ => true);
var array = [];
array[10] = 0;
array[20] = 0;
array[30] = 0;
console.log(array.filter(_ => true).join('|'))
Super simple function:
function reindex_array_keys(array, start){
var temp = [];
start = typeof start == 'undefined' ? 0 : start;
start = typeof start != 'number' ? 0 : start;
for(var i in array){
temp[start++] = array[i];
}
return temp;
}
testArray = reindex_array_keys(testArray);
Note: this will blow away any custom keys. the result will always be numerically indexed. you could add in checks for if it's an array or not but i tend to just not use functions i build other than they are intended to be used. you can also start the index higher if you like:
testArray = reindex_array_keys(testArray, 3);
which will produce 3 'undefined' items at the beginning of the array. you can then add to it later but i think it would be better to do testArray.unshift('newValue')
first then reindex personally.
have fun
var testArray = new Array();
testArray[3] = "qwerty";
testArray[7] = "asdfgh";
testArray[13] = "zxcvbn";
var isEmpty = function(x) {
// returns true if x is null and false if it is not.
if(x!=null){
return true;
}else{
return false
}
}
var newArray=testArray.filter(isEmpty);
var testString2 = newArray.join();
$('#write').text(testString2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With