This is kind of confusing so I will just demonstrate. I would like to create a function that takes an array like this:
[1, 2, 3, 4, 5, 6, 7]
...and returns an array of arrays like this:
[[1, 5], [2, 6], [3, 7], [4]]
assuming that the user wanted 4 groups. Notice that the elements are added to each group before 5th element is added back to the first.
Is there a simple way to do this? My project is using webpack, so I am open to ES6+ or even lodash. Thanks!
My proposal is:
var numGroups= 4;
var result = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].reduce(function(acc, ele, idx) {
var i = idx % numGroups;
(acc[i] == undefined) ? acc[i] = [ele] : acc[i].push(ele);
return acc;
}, []);
console.log(result);
Related
var numbers = [1, 2, 3, 4, 5, 6, 7, 8];
need to check if 2, 3, 4 exist in the array. Only has to be 1 of these numbers to return true...not all. What's the best approach. I was thinking lodash includes, but I believe I can only pass in a single value.
Using Array#some and Array#includes:
const hasAny = (arr = [], nums = []) =>
nums.some(n => arr.includes(n));
console.log( hasAny([1, 2, 3, 4, 5, 6, 7, 8], [2, 3, 4]) );
If you want to get the intersection, you can filter the first array by the second array, or rather, whether or not the second array contains each element of the first. You can see which members were included, or check the size of the new array > 0 if you need a boolean (or use Mr Badawi's .some method).
var numbers = [1, 2, 3, 4, 5, 6, 7, 8];
var needs = [2, 3, 4 ];
var check = numbers.filter(x=>needs.includes(x));
console.log(check);
var numbers = [3, 5, 6, 7, 8, 12, 20];
var needs = [2, 3, 4 ];
var check = numbers.filter(x=>needs.includes(x));
console.log(check);
With an array of: [1, 2, 3, 4, 5, 6]
I would like to delete between 2 indices such as 2 and 4 to produce [1, 2, null, null, 5, 6]. What's the easiest way to do this?
Hopefully better than this:
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
let i = 2;
const rangeEnd = 9;
while (i < rangeEnd) {
delete array[i];
i++;
}
console.log(array)
If you want to use some native API you can actually do this with splice(). Otherwise, you should iterate a for loop through your array and change the value in each iteration.
Here is an example of how it would be done:
const array = [1, 2, 3, 4, 5, 6]
array.splice(3, 2, null, null) // the First element is beginning index and the second is count one will indicate how many indexes you need to traverse from the first one, then you should provide replace element for each of them.
console.log(array)
Note: For more info about it you can read more here.
There is a possible workaround for large scale replacement, so I will give it a touch here:
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var anotherArr = Array(2).fill(null); // or you can simply define [null, null, ...]
Array.prototype.splice.apply(arr, [3, anotherArr.length].concat(anotherArr));
console.log(arr);
As you mean the range (2, 4] so you can follow this:
The range is: lower limit exclusive and the upper limit inclusive.
const arr = [1, 2, 3, 4, 5, 6];
const deleteRange = (arr, f, t) => {
return arr.map((item, i) => {
if (i + 1 > f && i + 1 <= t) {
return null;
}
return item;
})
}
console.log(deleteRange(arr, 2, 4));
What I expect from this code is [1, 2, 5, 6, 7, 8, 9]. The result is [7, 8, 9]. Where is my mistake? Jsfiddle for this. Thanks.
var ar = [1, 2, 3, 4, 5, 6],
arWrapper = _(ar),
removedTypes = [3, 4],
addedTypes = [7, 8, 9];
_.each(removedTypes, function (removedType) {
arWrapper = arWrapper.remove(function (type) {
return type === removedType;
});
});
_.each(addedTypes, function (addedType) {
arWrapper = arWrapper.push(addedType);
});
console.log("%O", arWrapper.value());
Your solution is not "idiomatic", since it relies on free variables here and there (and is "too imperative" overall):
The "better" solution would be:
var result = arWrapper.difference(removedTypes).union(addedTypes);
JSFiddle: http://jsfiddle.net/xb90agw2/1/
As of your solution - it does not work because .remove() returns a collection of removed elements, not the collection with filtered out elements as you assume.
Below are several stabs at removing 3 from the array [8,2,3,4] using lodash. The elegant syntax for removing an object from an array of objects makes me wonder if I just haven't figured out the right approach here.
> _.remove([8,2,3,4], 3)
[]
> x = [8,2,3,4]
[8, 2, 3, 4]
> _.remove(x, 3)
[]
> x
[8, 2, 3, 4]
> _.remove(x, {3: true})
[]
> x
[8, 2, 3, 4]
> _.remove(x, [3])
[]
> x
[8, 2, 3, 4]
> _.remove(x, function(val) { return val === 3; });
[3]
> x
[8, 2, 4]
Is there another way to remove a matching element from an array that would be similar to _.remove(arrayOfObjs, {id:3})
Yes, but not using remove. You can instead use pull to remove values from an array:
Removes all provided values from array using SameValueZero for equality comparisons.
// pull modifies its argument:
x = [8, 2, 3, 4]
_.pull(x, 3)
x // => [8, 2, 4]
// pull also returns the modified array:
y = _.pull([1, 2, 3, 4, 5], 2, 3) // => [1, 4, 5]
I am now writing a javascript to filter out arrays that contains a specific sub-array. Of course I can write the function by myself, but I am just curious if there are already some built-in function in javascript or other javascript library to do that, or if there are easy way to do that with just a few lines.
I found that I can use underscore.js if all the elements in the sub-array is unique. There is a intersection function and I can check the lenght after intersection to see if the length are correct. However, that function fails if there are repeated values in the sub-array.
For example,
_.intersection([1, 2, 3, 4, 5], [2, 1]);
This will return [1, 2] and by checking the length I will know this array contains the sub-array.
However, when there are repeated values in the sub-array,
_.intersection([1, 1, 2, 3, 4, 7, 10], [1, 1, 2]);
_.intersection([1, 2, 3, 4], [1, 1, 2]);
Both will return [1, 2] and the cases cannot be distinguished.
Is there other pre-built function I can use or is there a easy way to do the job within a few lines?
Try this:
function contains(a, b) {
// sort arguments
if(a.length < b.length) {
var temp = a;
a = b;
b = temp;
}
// copy array
a = a.slice();
return b.every(function(elm) {
var index = a.indexOf(elm);
if(index !== -1) {
// remove the found element
a.splice(index, 1);
return true;
}
return false;
});
}
console.log(contains([1, 1, 2], [1, 2, 3, 4, 7, 10])); // logs false
console.log(contains([1, 1, 2], [1, 1, 2, 3, 4, 7, 10])); // logs true
console.log(contains([1, 2, 3, 4, 7, 10], [1, 1, 2])); // logs false
console.log(contains([1, 1, 2, 3, 4, 7, 10], [1, 1, 2])); // logs true
Here is the demo