How do you efficiently scroll an array with looping either acting on the array itself or returning a new array
arr = [1,2,3,4,5]
i want to do something like this:
arr.scroll(-2)
arr now is [4,5,1,2,3]
Use Array.slice:
> arr.slice(-2).concat(arr.slice(0, -2));
[4, 5, 1, 2, 3]
You can then generalize it and extend Array.prototype with the scroll function:
Array.prototype.scroll = function (shift) {
return this.slice(shift).concat(this.slice(0, shift));
};
> arr.scroll(-2);
[4, 5, 1, 2, 3]
Related
I'm trying to populate an array with all values retrieved (I am in a loop) and then to delete values already present in the yearsJustSelected array:
yearsJustSelected.forEach((el:any)=>{
yearsJustSelectedAll.push(el);
});
yearsJustSelectedFiltered = yearsJustSelected.filter((el: any) => !yearsJustSelectedAll.includes(el));
For example:
loop 1 --> yearsJustSelected=[1, 2, 3] - yearsJustSelectedAll=[1, 2, 3] - yearsJustSelectedFiltered = []
loop 2 --> yearsJustSelected=[2, 3, 4, 5] - yearsJustSelectedAll=[1, 2, 3, 4, 5] - yearsJustSelectedFiltered=[4, 5] --> because [2, 3 ] were already present in the yearsJustSelectedAll in the first loop
This code:
yearsJustSelectedFiltered = yearsJustSelected.filter((el: any) => !yearsJustSelectedAll.includes(el));
always return an empty array.
yearsJustSelected.forEach((el:any)=>{
yearsJustSelectedAll.push(el);
});
// The code above will add all elements in yearsJustSelected into yearsJustSelectedAll
// making the two arrays the same.
There are no instances where one array has elements that are not in the other array
You could check if the target array does not have the item. Then push the item to the target array.
function select(source, target) {
source.forEach(v => target.includes(v) || target.push(v));
}
var yearsJustSelectedAll = [];
select([1, 2, 3], yearsJustSelectedAll);
console.log(...yearsJustSelectedAll);
select([2, 3, 4, 5], yearsJustSelectedAll);
console.log(...yearsJustSelectedAll);
select([4, 5], yearsJustSelectedAll);
console.log(...yearsJustSelectedAll);
Why the answer is undefined in the Second example?
// First
var arr = [
[1, 4, 6],
['alex']
];
var newArr = arr[1];
newArr.push('Peter');
console.log(arr);
// Second
var arr = [
[1, 4, 6],
['alex']
];
arr.push([1]['Peter']);
console.log(arr);
The code [1]['Peter'] is trying to access a key named Peter from the array literal [1]. And it is undefined
Your code is equivalent to this:
var arr = [
[1, 4, 6],
['alex']
];
var tempArray = [1];
var tempValue = tempArray['Peter'] // undefined
arr.push(tempValue);
console.log(arr);
You should change it to: arr[1].push('Peter')
That syntax [1]['Peter'] doesn't do what you might imagine. You're passing an input parameter. It doesn't reference the array pushing it into, it's completely independent. So you're effectively telling JavaScript to first create a new array ([1]), and then try to access an index called "Peter" from within it (["Peter"]), and then push that into the next free index in arr. Clearly that "Peter" index doesn't exist within the new array, which is why it outputs undefined.
Instead you'd have to write it like this, so it pushes to the existing array, which is itself at index 1 of arr:
// First
var arr = [
[1, 4, 6],
['alex']
];
var newArr = arr[1];
newArr.push('Peter');
console.log(arr);
// Second
var arr = [
[1, 4, 6],
['alex']
];
arr[1].push('Peter');
console.log(arr);
The problem is
arr.push([1]['Peter']);
But an array which contains a single element, 1, does not have the property Peter. Arrays generally do not have non-numeric properties (other than those on Array.prototype and Object.prototype)
All you need to do is
var arr = [
[1, 4, 6],
['alex']
];
arr[1].push('Peter');
console.log(arr);
I want to iterate over an array in reverse order using Lodash. Is that possible?
My code looks like below.
var array = [1, 2, 3, 4, 5];
_.each(array, function(i) {
_.remove(array, i);
});
When I do _.pullAt(array, 0) new array is [2, 3, 4, 5]. All array elements shifted to left by 1 position, and current index is pointing to element 3. After next iteration, 3 will get deleted and then 5. After 3rd iteration array contains [2, 4] which I couldn't delete.
If I iterate over the array in reverse order, then this problem won't arise. In Lodash, can I iterate over an array in reverse order?
You can use _.reverse available in version 4:
var array = [1, 2, 3, 4, 5];
array = _.reverse(array)
console.log(array)
//5, 4, 3, 2, 1
See How do I empty an array in JavaScript? if you only want that and choose your weapon.
Otherwise, strictly answering the question, to iterate the indices from the length of the array to zero, you could use the "down-to" --> operator1. But that's not really necessary, even underscore isn't necessary in this case as the .pop function is enough.
var arr = [1, 2, 3, 4, 5],
index = arr.length;
while (index --> 0) {
console.log(index);
arr.pop();
}
console.log(arr);
If you're using Lodash like the functions referenced in the question seem to indicate, you could use _.eachRight.
var arr = [1, 2, 3, 4, 5];
_.eachRight(arr, function(value) {
console.log(value);
arr.pop();
});
console.log(arr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js"></script>
1 The down-to operator doesn't exist and is only a -- decrement followed by a > comparison.
I want to take an array [1, 2, 3] and return [1, 2, 3, 1].
I'm using Ramda, and I can get the desired result like this:
const fn = arr => R.append(R.prop(0, arr), arr);
But I'd like to do it point-free. Here's the closest I've gotten:
const fn = R.compose(R.append, R.prop(0));
fn(arr)(arr)
But that looks silly. What am I missing? Thanks!
converge can be very helpful for things like this.
const rotate = R.converge(R.append, [R.head, R.identity])
rotate([1, 2, 3]); //=> [1, 2, 3, 1]
The S combinator is useful here:
S.S(S.C(R.append), R.head, [1, 2, 3]);
// => [1, 2, 3, 1]
I have a numeric javascript array, that contains several objects with geodata in it.
What I need to do is, to add a dynamic count of new objects after a specific object in this array.
I know, that there is the splice function, but i do not know, how to make the count of new objects variable.
myArray.splice( pos, 0, ... );
What am I getting wrong?
Hope I understood what you meant.
var
oldA = [1, 2, 3],
newA = [4, 5];
oldA.splice.apply(oldA, (function (index, howMany, elements) {
// this is actually building the arguments array (2nd parameter)
// for the oldA.splice call
elements = elements.slice();
elements.splice(0, 0, index, howMany);
return elements;
}(/*index to insert at*/ 2, /*howMany to remove*/ 0, /*elements to insert*/ newA)));
console.log(oldA, newA); // [1, 2, 4, 5, 3] [4, 5]