For the life of me I can't figure out how to duplicate the numbers array.
Expected result: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
Here is my code so far:
const numbers = [1, 2, 3, 4, 5];
var result = numbers.map((number) => {
return number
});
console.log(result);
I can't figure out how you can take the numbers array and then duplicate the array?
I was starting to do if statements - "If number is equal to 1 then return 1" but that would print the numbers like this [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
https://jsfiddle.net/e6jf74n7/1/
Map will map all values one-to-one, that's why it's called "map"; it gives you one value, you return a value that should replace it.
To duplicate a list, concat the list to itself:
const numbers = [1, 2, 3, 4, 5];
var result = numbers.concat(numbers);
console.log(result);
fastest way is to use slice() then concat() to old array.
var arr = [ 1, 2, 3, 4, 5 ];
var clone = arr.slice(0);
var duplicate = arr.concat(clone);
Map won't work in this case just use concat
numbers.concat(numbers);
If you want to concat multiple times then
var concatArr = numbers;
for (var i=0; i < 9 ; i++ ) {
numbers = numbers.concat(concatArr);
}
console.log(numbers);
Concat docs
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
Related
Write a function that takes two arrays of integers (nums and index) and
returns a target array under the following rules:
Initially target array is empty.
From left to right read nums[i] and index[i], insert at index index[i] the
value nums[i] in target array.
Repeat the previous step until there are no elements to read in nums and index.
Example 1
Input: nums = [0, 1, 2, 3, 4], index = [0, 4, 1, 2, 3]
Output: [0, 4, 1, 2, 3]
Example 2
Input: nums = [1, 2, 3, 4, 0], index = [0, 1, 2, 3, 0]
Output: [1, 2, 3, 4, 1]
Your example does not match your description. According to your example, you want to insert the nums[index[i]] in your target array's i'th position.
You can do this using Javascript's array.map function like this -
const targetArray = index.map(i => nums[i]);
You may need to add necessary sanity checks as well depending on the contexts.
You can simply achieve this by using Array.forEach() along with Array.splice() method.
Live Demo :
const numsArr = [1, 2, 3, 4, 0];
const indexArr = [0, 1, 2, 3, 0];
let outputArr = [];
numsArr.forEach((elem, index) => {
outputArr.splice(index, 0, numsArr[indexArr[index]]);
});
console.log(outputArr);
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));
I have an array. It looks like below:
var num = [2,1,2,6,2,4];
I would like to sort but return index to an array:
var result = [1, 0, 2, 4, 5, 3];
If there is a same number, the smaller index of should be placed first.
You could get all indices and sort with the values of num.
var num = [2, 1, 2, 6, 2, 4],
indices = [...num.keys()].sort((a, b) => num[a] - num[b]);
console.log(indices); // [1, 0, 2, 4, 5, 3]
previousValue currentValue index array return value
first call 0 1 1 [0, 1, 2, 3, 4] 1
second call 1 2 2 [0, 1, 2, 3, 4] 3
third call 3 3 3 [0, 1, 2, 3, 4] 6
fourth call 6 4 4 [0, 1, 2, 3, 4] 10
I want 1,3,6,10 in an array not the return total 10. So to return each call
You can push the return value into an array, like this. It goes against functional programming since it mutates results as a side effect. But it does meet your needs.
var array = [0, 1, 2, 3, 4];
var results = [];
array.reduce(function(previousValue, currentValue) {
var newValue = previousValue + currentValue;
results.push(newValue);
return newValue;
});
// result is 1,3,6,10
alert(results);
Don't use reduce for this. Slice the array, shift a value to start a subtotal, then use map.
var arr = [0, 1, 2, 3, 4], output = arr.slice(), subtotal = output.shift()
output = output.map(function(elem) { return subtotal += elem })
// output is [1, 3, 6, 10]
Edit - Actually, this could work fine with reduce, and even more concise than the above:
var arr = [0, 1, 2, 3, 4]
arr.reduce(function(a, b, ndx) { return a.length ? a.concat(a[ndx - 2] + b) : [a + b]})
// returns [1, 3, 6, 10]