javascript push an item into list index by value - javascript

Lets say I have a list:
var list = []
Now I want to insert some value into the list in a way that its index should be according to its value
list.push(6)
list.push(2)
This will give me a result of [6, 2]
But what I want is its index should be managed according to its value.
Here 2 is smaller than 6 so 2 should come first and give result like
[2, 6] . And if again I do list.push(1) it should give result like [1,2,6] and so on.
How can I achieve this in javascript ?

You can use Array#sort.
var list = [];
list.push(6);
list.push(2);
list.push(1);
list.sort(function(a, b) {
return a - b;
});
console.log(list);

You simply need to use Array sort function. As it treats array items as string and does not sort integers correctly by default, you need to use custom compare function for this:
var arr = [1, 22, 6, 2 ];
arr.sort(function(a, b){
return a - b;
});
console.log( arr );

You can use the sort() function.
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
The result of fruits will be:
Apple,Banana,Mango,Orange
Edit:
Sorry, for numbers this solution does not work. You can use this:
Sort numbers in an array in ascending order:
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a-b});
The result of points will be:
1,5,10,25,40,100

We can override push function of list object.
var list = [];
var copyPush = list.push.bind(list);
list.push = function(){
var toret = copyPush.apply(this,arguments);
this.sort(function(a,b){ return a-b});
return toret;
}
list.push(1);
list.push(3);
list.push(2);
list.push(5);
console.log(list); //[1, 2, 3, 5]

These other answers are missing the point, they assume that the list is already populated, and then you push in. What you need to do is compare the number you are going to push into the array, with the array itself, splicing the value in like so:
arr = [1,5,7];
var number = 0;
function findValue(value) {
return function(element, index, array) {
return (element <= value);
}
}
var filtered = arr.filter(findValue(number))
index = arr.indexOf(filtered[filtered.length-1]) +1;
arr.splice(index, 0, number);
alert(arr);
Here's a working fiddle:
https://jsfiddle.net/GerardSimpson/h3t6vcka/

Related

How to get item from multiple consecutive arrays

I am having a little difficuly coming up with a solution to my problem.
I am trying to get the item at an index of a collection of arrays. I cannot actually concatenate the arrays, they need to stay seperate.
Normally, to get the 3rd item from an array, you would do:
function getItemFromJustOneArray(index){
var my_array = [1,2,3,4,5,6];
return my_array[index];
}
getItemFromJustOneArray(2); // returns 3
However, I have a bunch of arrays (could be any amount of arrays) and these arrays cannot be merged into one.
function getItemFromMultipleArrays(index){
var array1 = [1,2];
var array2 = [3,4,5];
var array3 = [6];
// I cannot use concat (or similar) to merge the arrays,
// they need to stay seperate
// also, could be 3 arrays, but could also be 1, or 5...
// return 3;
}
getItemFromMultipleArrays(2); // SHOULD RETURN 3
I have tried a bunch of lines that loops over the array, but I cannot really get a working solution.
Does someone know an elegant solution to this problem?
Nest all the arrays in another array. Then loop over that array, decrementing index by each array's length until it's within the length of the current element. Then you can return the appropriate element of that nested array.
function getItemFromMultipleArrays(index) {
var array1 = [1, 2];
var array2 = [3, 4, 5];
var array3 = [6];
var all_arrays = [array1, array2, array3];
var i;
for (i = 0; i < all_arrays.length && index >= all_arrays[i].length; i++) {
index -= all_arrays[i].length;
}
if (i < all_arrays.length) {
return all_arrays[i][index];
}
}
console.log(getItemFromMultipleArrays(2)); // SHOULD RETURN 3
Why not spread the arrays to a new one and use the index for the value?
function getItemFromMultipleArrays(index) {
const
array1 = [1, 2],
array2 = [3, 4, 5],
array3 = [6];
return [...array1, ...array2, ...array3][index];
}
console.log(getItemFromMultipleArrays(2)); // 3
Another approach by using an offset for iterating arrays.
function getItemFromMultipleArrays(index) {
const
array1 = [1, 2],
array2 = [3, 4, 5],
array3 = [6],
temp = [array1, array2, array3];
let j = 0;
while (index >= temp[j].length) index -= temp[j++].length;
return temp[j][index];
}
console.log(getItemFromMultipleArrays(2)); // 3
console.log(getItemFromMultipleArrays(5)); // 6
this should do it, just copying all the arrays into a "big" one and accessing it (added a helping function)
// this function takes any amount of arrays and returns a new
// "concatted" array without affecting the original ones.
function connectArrays(...arrays) {
return [...arrays.flat()];
}
function getItemFromMultipleArrays(index) {
var array1 = [1,2];
var array2 = [3,4,5];
var array3 = [6];
var allArrays = connectArrays(array1, array2, array3);
return allArrays[index];
}
getItemFromMultipleArrays(2); // SHOULD RETURN 3

Take value from one array and add it on to the last value in another array

I have a problem with javascript arrays I am not sure how to approach.
First of all I want the second array's first value to be the same as the first value in the first array, and then add on to that.
I have an array and I want to add two values in the first array and push the result in to the second array, I then want to get the third value in the first array and add it to the last value in the second array, and then the fourth and fifth etc...
Example below because i'm finding it hard to explain!
var arr = [1, 2, 3, 4, 5];
var newArr = [];
End result of the second array (it's the result of adding consecutive values of the first array to the last value of the second array:
var newArr = [1, 3, 6, 10, 15];
I hope this makes sense - I'm finding it hard to think clearly about it!
This is a great candidate for reduce - you initialize you accumulator array with the first element of arr, and then you build your accumulator array as you iterate through the rest of the elements of arr:
var arr = [1, 2, 3, 4, 5];
var newArr = arr.reduce((acc, current) => {
acc.push((acc[acc.length - 1] || 0) + current);
return acc;
}, []);
console.log(newArr);
You can probably do it a smarter way using map/reduce or lodash, but the simplest option is a simple for loop:
var arr = [1, 2, 3, 4, 5];
var newArr = [];
for(let i = 0; i < arr.length; i++ ) { // iterate over input array
let incrementer = arr[i] // get value from input array
if( newArr[ newArr.length - 1 ] ) { // if the output array has a last value
incrementer += newArr[ newArr.length - 1 ] // add the last value
}
newArr.push(incrementer) // append the new value to end of output array
}
console.log(newArr)

How to create array as a property in javascript?

I want to create a function that generates random Integers from a certain interval and each time it is called, it should produce a unique one. For that I have created this generateUniqueInt.
function generateUniqueInt() {
var res = Math.floor(Math.random() * 100);
while (generateUniqueInt.used.indexOf(res) !== -1)
res = Math.floor(Math.random() * 100);
generateUniqueInt.used.push(res);
return res;
}
generateUniqueInt.used = new Array;
for (let i = 0; i < 20; i++) {
console.log(generateUniqueInt());
}
generateUniqueInt.used.sort();
console.log(generateUniqueInt.used);
I called this function few times like this and it works.
Then I wanted to check which values were actually generated and for easier inspection I sorted the used property. But as it seems, used is not an array anymore.
I have tried using generateUniqueInt.used = []; as well as Object.defineProperty but the outcome is the same each time. What am I missing here? Is there a way to create used as an array?
Your issue is :The default sort order is according to string Unicode code points.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
So below code will return following output:
var array1 = [1, 30, 4, 21];
array1.sort();
console.log(array1);
// expected output: Array [1, 21, 30, 4]
In order to solve for numbers you will may follow example from MDN:
var numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
return a - b;
});
console.log(numbers);
// [1, 2, 3, 4, 5]
So your example would become following which should give you correct desired output:
generateUniqueInt.used.sort(function(a,b){ return a - b});
By default the sort method sorts elements alphabetically, so your array will be sorted like [1,2,25,3,4 ...]
use this to sort an array of numbers :
generateUniqueInt.used.sort(function(a,b){ return a - b});
let arr = [4,2,65,12,23,5,3]
let sorted1 = arr.sort()
console.log('sorted alphabetically : ', JSON.stringify(sorted1))
let sorted2 = arr.sort(function(a,b){ return a - b})
console.log('sorted numerically : ', JSON.stringify(sorted2))

Only specific 'columns' from a 2d javascript array

I have an array in js:
myArray = [['1','2','3'],['4','5','6'],['7','8','9']];
How can I produce an array like this:
myAlteredArray = [['2','3'],['5','6'],['8','9']];
I basically want to exclude the first column out of the array.
Quick solution
The easiest way to do this would be to use map and slice.
var subsections = myArray.map(function (subarray) {
return subarray.slice(1)
})
You could also manipulate the subsections in any way you want by doing the following:
var subsections = myArray.map(function (subarray) {
var subsection = subarray.slice(1)
subsection[1] = parseInt(subsection[1], 10) // parse index 1 in base 10
return subsection
})
Array.prototype.map
myArray.map(callback) executes callback on each element in myArray and returns a new array made up of all the return values. For example:
[1, 2, 3].map(function (number) { return 10 - number })
would return [9, 8, 7] and leave the original array unchanged.
Array.prototype.slice
myArray.slice(start, [end]) will return a subsection of myArray in a new array. If you only pass start, end is assumed to be the end of the array. For example:
['dogs', 'cats', 'fish', 'lizards'].slice(2) == ['fish', 'lizards']
['dogs', 'cats', 'fish', 'lizards'].slice(1, 3) == ['cats', 'fish']
Fun fact: .slice() works on strings too!
You can do it like this, but note that this will alter original array:
myArray = [['1','2','3'],['4','5','6'],['7','8','9']];
myArray.filter(function(i){
return i.shift();
});
console.log(myArray);//logs [["2", "3"], ["5", "6"], ["8", "9"]]
var myArray = [['1','2','3'],['4','5','6'],['7','8','9']];
var filteredArray = myArray.map(function(currArray,v){
return currArray.slice(1);
})
console.log(filteredArray);
You should iterate through the array and use the slice function in each subarray to extract the first element.
var myArray = [['1','2','3'],['4','5','6'],['7','8','9']];
for (var i = arr.length - 1; i >= 0; i--) {
arr[i] = arr.slice(1);
}

Remove multiple elements from array in Javascript/jQuery

I have two arrays. The first array contains some values while the second array contains indices of the values which should be removed from the first array. For example:
var valuesArr = new Array("v1","v2","v3","v4","v5");
var removeValFromIndex = new Array(0,2,4);
I want to remove the values present at indices 0,2,4 from valuesArr. I thought the native splice method might help so I came up with:
$.each(removeValFromIndex,function(index,value){
valuesArr.splice(value,1);
});
But it didn't work because after each splice, the indices of the values in valuesArr were different. I could solve this problem by using a temporary array and copying all values to the second array, but I was wondering if there are any native methods to which we can pass multiple indices at which to remove values from an array.
I would prefer a jQuery solution. (Not sure if I can use grep here)
There's always the plain old for loop:
var valuesArr = ["v1","v2","v3","v4","v5"],
removeValFromIndex = [0,2,4];
for (var i = removeValFromIndex.length -1; i >= 0; i--)
valuesArr.splice(removeValFromIndex[i],1);
Go through removeValFromIndex in reverse order and you can .splice() without messing up the indexes of the yet-to-be-removed items.
Note in the above I've used the array-literal syntax with square brackets to declare the two arrays. This is the recommended syntax because new Array() use is potentially confusing given that it responds differently depending on how many parameters you pass in.
EDIT: Just saw your comment on another answer about the array of indexes not necessarily being in any particular order. If that's the case just sort it into descending order before you start:
removeValFromIndex.sort(function(a,b){ return b - a; });
And follow that with whatever looping / $.each() / etc. method you like.
I suggest you use Array.prototype.filter
var valuesArr = ["v1","v2","v3","v4","v5"];
var removeValFrom = [0, 2, 4];
valuesArr = valuesArr.filter(function(value, index) {
return removeValFrom.indexOf(index) == -1;
})
Here is one that I use when not going with lodash/underscore:
while(IndexesToBeRemoved.length) {
elements.splice(IndexesToBeRemoved.pop(), 1);
}
Not in-place but can be done using grep and inArray functions of jQuery.
var arr = $.grep(valuesArr, function(n, i) {
return $.inArray(i, removeValFromIndex) ==-1;
});
alert(arr);//arr contains V2, V4
check this fiddle.
A simple and efficient (linear complexity) solution using filter and Set:
const valuesArr = ['v1', 'v2', 'v3', 'v4', 'v5'];
const removeValFromIndex = [0, 2, 4];
const indexSet = new Set(removeValFromIndex);
const arrayWithValuesRemoved = valuesArr.filter((value, i) => !indexSet.has(i));
console.log(arrayWithValuesRemoved);
The great advantage of that implementation is that the Set lookup operation (has function) takes a constant time, being faster than nevace's answer, for example.
This works well for me and work when deleting from an array of objects too:
var array = [
{ id: 1, name: 'bob', faveColor: 'blue' },
{ id: 2, name: 'jane', faveColor: 'red' },
{ id: 3, name: 'sam', faveColor: 'blue' }
];
// remove people that like blue
array.filter(x => x.faveColor === 'blue').forEach(x => array.splice(array.indexOf(x), 1));
There might be a shorter more effecient way to write this but this does work.
It feels necessary to post an answer with O(n) time :). The problem with the splice solution is that due to the underlying implementation of array being literally an array, each splice call will take O(n) time. This is most pronounced when we setup an example to exploit this behavior:
var n = 100
var xs = []
for(var i=0; i<n;i++)
xs.push(i)
var is = []
for(var i=n/2-1; i>=0;i--)
is.push(i)
This removes elements starting from the middle to the start, hence each remove forces the js engine to copy n/2 elements, we have (n/2)^2 copy operations in total which is quadratic.
The splice solution (assuming is is already sorted in decreasing order to get rid of overheads) goes like this:
for(var i=0; i<is.length; i++)
xs.splice(is[i], 1)
However, it is not hard to implement a linear time solution, by re-constructing the array from scratch, using a mask to see if we copy elements or not (sort will push this to O(n)log(n)). The following is such an implementation (not that mask is boolean inverted for speed):
var mask = new Array(xs.length)
for(var i=is.length - 1; i>=0; i--)
mask[is[i]] = true
var offset = 0
for(var i=0; i<xs.length; i++){
if(mask[i] === undefined){
xs[offset] = xs[i]
offset++
}
}
xs.length = offset
I ran this on jsperf.com and for even n=100 the splice method is a full 90% slower. For larger n this difference will be much greater.
I find this the most elegant solution:
const oldArray = [1, 2, 3, 4, 5]
const removeItems = [1, 3, 5]
const newArray = oldArray.filter((value) => {
return !removeItems.includes(value)
})
console.log(newArray)
output:
[2, 4]
or even shorter:
const newArray = oldArray.filter(v => !removeItems.includes(v))
function filtermethod(element, index, array) {
return removeValFromIndex.find(index)
}
var result = valuesArr.filter(filtermethod);
MDN reference is here
In pure JS you can loop through the array backwards, so splice() will not mess up indices of the elements next in the loop:
for (var i = arr.length - 1; i >= 0; i--) {
if ( yuck(arr[i]) ) {
arr.splice(i, 1);
}
}
A simple solution using ES5. This seems more appropriate for most applications nowadays, since many do no longer want to rely on jQuery etc.
When the indexes to be removed are sorted in ascending order:
var valuesArr = ["v1", "v2", "v3", "v4", "v5"];
var removeValFromIndex = [0, 2, 4]; // ascending
removeValFromIndex.reverse().forEach(function(index) {
valuesArr.splice(index, 1);
});
When the indexes to be removed are not sorted:
var valuesArr = ["v1", "v2", "v3", "v4", "v5"];
var removeValFromIndex = [2, 4, 0]; // unsorted
removeValFromIndex.sort(function(a, b) { return b - a; }).forEach(function(index) {
valuesArr.splice(index, 1);
});
Quick ES6 one liner:
const valuesArr = new Array("v1","v2","v3","v4","v5");
const removeValFromIndex = new Array(0,2,4);
const arrayWithValuesRemoved = valuesArr.filter((value, i) => removeValFromIndex.includes(i))
If you are using underscore.js, you can use _.filter() to solve your problem.
var valuesArr = new Array("v1","v2","v3","v4","v5");
var removeValFromIndex = new Array(0,2,4);
var filteredArr = _.filter(valuesArr, function(item, index){
return !_.contains(removeValFromIndex, index);
});
Additionally, if you are trying to remove items using a list of items instead of indexes, you can simply use _.without(), like so:
var valuesArr = new Array("v1","v2","v3","v4","v5");
var filteredArr = _.without(valuesArr, "V1", "V3");
Now filteredArr should be ["V2", "V4", "V5"]
You can correct your code by replacing removeValFromIndex with removeValFromIndex.reverse(). If that array is not guaranteed to use ascending order, you can instead use removeValFromIndex.sort(function(a, b) { return b - a }).
Here's one possibility:
valuesArr = removeValFromIndex.reduceRight(function (arr, it) {
arr.splice(it, 1);
return arr;
}, valuesArr.sort(function (a, b) { return b - a }));
Example on jsFiddle
MDN on Array.prototype.reduceRight
filter + indexOf (IE9+):
function removeMany(array, indexes) {
return array.filter(function(_, idx) {
return indexes.indexOf(idx) === -1;
});
});
Or with ES6 filter + find (Edge+):
function removeMany(array, indexes = []) {
return array.filter((_, idx) => indexes.indexOf(idx) === -1)
}
Here's a quickie.
function removeFromArray(arr, toRemove){
return arr.filter(item => toRemove.indexOf(item) === -1)
}
const arr1 = [1, 2, 3, 4, 5, 6, 7]
const arr2 = removeFromArray(arr1, [2, 4, 6]) // [1,3,5,7]
Try this
var valuesArr = new Array("v1", "v2", "v3", "v4", "v5");
console.info("Before valuesArr = " + valuesArr);
var removeValFromIndex = new Array(0, 2, 4);
valuesArr = valuesArr.filter((val, index) => {
return !removeValFromIndex.includes(index);
})
console.info("After valuesArr = " + valuesArr);
Sounds like Apply could be what you are looking for.
maybe something like this would work?
Array.prototype.splice.apply(valuesArray, removeValFromIndexes );
var valuesArr = new Array("v1","v2","v3","v4","v5");
var removeValFromIndex = new Array(0,2,4);
console.log(valuesArr)
let arr2 = [];
for (let i = 0; i < valuesArr.length; i++){
if ( //could also just imput this below instead of index value
valuesArr[i] !== valuesArr[0] && // "v1" <--
valuesArr[i] !== valuesArr[2] && // "v3" <--
valuesArr[i] !== valuesArr[4] // "v5" <--
){
arr2.push(valuesArr[i]);
}
}
console.log(arr2);
This works. However, you would make a new array in the process. Not sure if thats would you want or not, but technically it would be an array containing only the values you wanted.
You can try Lodash js library functions (_.forEach(), _.remove()). I was using this technique to remove multiple rows from the table.
let valuesArr = [
{id: 1, name: "dog"},
{id: 2, name: "cat"},
{id: 3, name: "rat"},
{id: 4, name: "bat"},
{id: 5, name: "pig"},
];
let removeValFromIndex = [
{id: 2, name: "cat"},
{id: 5, name: "pig"},
];
_.forEach(removeValFromIndex, (indi) => {
_.remove(valuesArr, (item) => {
return item.id === indi.id;
});
})
console.log(valuesArr)
/*[
{id: 1, name: "dog"},
{id: 3, name: "rat"},
{id: 4, name: "bat"},
];*/
Don't forget to clone (_.clone(valuesArr) or [...valuesArr]) before mutate your array
You could try and use delete array[index] This won't completely remove the element but rather sets the value to undefined.
removeValFromIndex.forEach(function(toRemoveIndex){
valuesArr.splice(toRemoveIndex,1);
});
For Multiple items or unique item:
I suggest you use Array.prototype.filter
Don't ever use indexOf if you already know the index!:
var valuesArr = ["v1","v2","v3","v4","v5"];
var removeValFrom = [0, 2, 4];
valuesArr = valuesArr.filter(function(value, index) {
return removeValFrom.indexOf(index) == -1;
}); // BIG O(N*m) where N is length of valuesArr and m is length removeValFrom
Do:
with Hashes... using Array.prototype.map
var valuesArr = ["v1","v2","v3","v4","v5"];
var removeValFrom = {};
([0, 2, 4]).map(x=>removeValFrom[x]=1); //bild the hash.
valuesArr = valuesArr.filter(function(value, index) {
return removeValFrom[index] == 1;
}); // BIG O(N) where N is valuesArr;
You could construct a Set from the array and then create an array from the set.
const array = [1, 1, 2, 3, 5, 5, 1];
const uniqueArray = [...new Set(array)];
console.log(uniqueArray); // Result: [1, 2, 3, 5]

Categories

Resources