How to delete the first argument in arguments object? - javascript

I need to remove the first item in the arguments object so that my let args variable equals to all the following arguments.
How can I do it?
function destroyer(arr) {
let myArr = arguments[0];
let args = arguments;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);

Use slice to retrieve the arguments past the first, and use rest parameters instead of a single arr in your function argument, if you can - many linters recommend against using arguments, and using that keyword is not necessary here:
function destroyer(...args) {
const otherArgs = args.slice(1);
console.log('length: ' + otherArgs.length, 'items: ' + otherArgs);
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
If you want a reference to the first argument as well, use rest parameters after collecting the first arr into a variable:
function destroyer(arr, ...otherArgs) {
console.log('arr: ' + arr);
console.log('length: ' + otherArgs.length, 'items: ' + otherArgs);
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);

Simplest way:
function destroyer(...arr) {
arr.shift();
console.log( arr ); // 2,3
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);

Related

Remove minimum value from Array, but if duplicate remove only once

I have the following function which is supposed to remove the smallest value from an array, but if this is a duplicate it will just remove the first one and leave the others.
var array1 = [1, 2, 3, 4, 5];
var array2 = [5, 3, 2, 1, 4];
var array3 = [2, 2, 1, 2, 1];
function removeSmallest(numbers) {
return numbers.filter(function(elem, pos, self) {
if(elem == Math.min.apply(null, numbers) && pos == self.indexOf(elem)) {
// Remove element from Array
console.log(elem, pos);
numbers.splice(pos, 1);
};
return numbers;
});
};
Via console.log(elem, pos) I understand that I have correctly identified the smallest and first element in the array, but when I try to remove it through splice(), I end up getting the following result for the arrays:
array1 = [1, 3, 4, 5]; // But I expected [2, 3, 4, 5]
array2 = [5, 3, 2, 1]; // But I expected [5, 3, 2, 4]
array3 = [2, 2, 1, 1]; // But I expected [2, 2, 2, 1]
Do you know what is the issue with my code? Thanks in advance for your replies!
function removeSmallest(numbers) {
const smallest = Math.min.apply(null, numbers);
const pos = numbers.indexOf(smallest);
return numbers.slice(0, pos).concat(numbers.slice(pos + 1));
};
You shouldn't use filter() the way you do. It's also a good practice that a function should return a new array rather than modifying the existing one and you definitely shouldn't modify an array while iterating over it.
function removeSmallest(arr){
var temp=arr.slice(),smallElement=null;
temp.sort(sortReverse);
smallElement=temp[temp.length-1];
var position=arr.indexOf(smallElement);
arr.splice(pos,1);
console.log(arr);
}
function sortReverse (a,b){
if(a<b){return 1}
else if(a>b){return -1;}
else{return 0;}
}
var array1 = [1, 2, 3, 4, 5];
removeSmallest(array1);

Javascript parameters passing

This is the code I have been given. I looked around and I don't quite understand.
This is my question function destroyer accepts one parameter an array but when its being called 3 parameters are sent: an array and 2 integers.
How can I access the two integer parameters in the function if they haven't been passed? Is there something in Javascript that would allow this?
function destroyer(arr) {
// Remove all the value;
return arr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
You can use arguments variable in your function to get a list of passed arguments.
// ES5
function destroyer(arr) {
var pieces = Array.prototype.splice.call(arguments, 1);
var i = 0;
while (arr[i]) {
-1 === pieces.indexOf(arr[i]) ? i++ : arr.splice(i, 1);
}
return arr;
}
// ES6
function destroyer2(arr, ...pieces) {
var i = 0;
while (arr[i]) {
-1 === pieces.indexOf(arr[i]) ? i++ : arr.splice(i, 1);
}
return arr;
}
console.log(JSON.stringify(destroyer([1, 2, 3, 1, 2, 3], 3, 1)));
console.log(JSON.stringify(destroyer2([1, 2, 3, 1, 2, 3], 2, 3)));

Javascript VArgs - not understanding arguments objects with unknown input

So this is a bit of a newbie troubleshooting question. I am doing an exercise of freecodecamp, and I am having an issue parsing the input to my function. It's short, and I think I can cut to the chase if I just show you the code:
function destroyer(arr) {
// Remove all the values
console.log("---");
console.log("arr: " + arr);
var args = Array.from(arr);
console.log(args);
var in_i = arr[0];
return in_i.filter(function (x) {
if (args.indexOf(x) !== -1) {
return true;
} else {
return false;
}
});
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
which gives me in the console (and I think this is the strange part):
---
arr: 1,2,3,1,2,3
[1, 2, 3, 1, 2, 3]
Clearly I'm not understanding something about arguments objects, or else something is broken. In my experience, the latter is exceedingly uncommon. I would have expected the Array.from(arr) to give an array object: [[1, 2, 3, 1, 2, 3], 2, 3].
The function function destroyer(arr) accepts only 1 parameter in the function destroyer which is an array [1, 2, 3, 1, 2, 3] and ignore the other arguments 2, 3. So, the arr is [1, 2, 3, 1, 2, 3].
If you need to access all the parameters passed to the function, then you can make use of arguments object which is an array like object. arguments would point to the array [[1, 2, 3, 1, 2, 3], 2, 3]. Following code should display the Arguments passed.
function destroyer(arr, param2, param3) {
// Remove all the values
console.log(arguments);
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
If your function takes 3 parameters as shown below, param2, param3 have been added, then you can access the value 2, 3 inside your function.
function destroyer(arr, param2, param3) {
// Remove all the values
console.log("---");
console.log("arr: " + arr);
var args = Array.from(arr);
console.log(args);
var in_i = arr[0];
return in_i.filter(function (x) {
if (args.indexOf(x) !== -1) {
return true;
} else {
return false;
}
});
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
In fact, the part that confused me was that on MDN, the use of the arguments variable was not just illustrative, it's a built-in variable. After #Agalo pointed out that the values I was getting on the console were just the array, it clicked for me.
The solution was to access the extra arguments through the built-in arguments object which automatically has the (reserved) name arguments. Code is like so (for completeness, I should note that I also had false and true switched in the return statements):
function destroyer(arr) {
// Remove all the values
var args_l = arguments.length;
var args = Array.from(arguments);
console.log(args);
var in_i = args.shift();
return in_i.filter(function (x) {
if (args.indexOf(x) !== -1) {
return false;
} else {
return true;
}
});
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
To riff off of Agalo, you'll note you don't actually need the param2, param3 arguments in the function definition to get the exact same output:
function destroyer(arr) {
// Remove all the values
console.log(arguments);
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
At the risk of putting too fine a point on it:
function args_tester(a) {
console.log("a: " + a);
console.log("arguments: " + arguments);
console.log("arguments_as_array: " + Array.from(arguments));
for (var i = 0; i < arguments.length; i++) {
console.log(arguments[i]);
}
}
args_tester("a", "b", "c");

JavaScript - Array passed by reference but lost when reassigned

In the following JS code, why doesn't f3(arr2) change the value of arr2 like f2(arr1) did to arr1? Is there any way to make f3 work as expected (if possible, without returning the modified array)?
var arr1 = [1, 2, 3, 4];
var arr2 = [1, 2, 3, 4];
function f1() {
return [2, 3, 4, 5];
}
function f2(arr) {
arr.push(5);
}
function f3(arr) {
arr = f1();
}
f2(arr1);
console.log(arr1); // [ 1, 2, 3, 4, 5 ]
f3(arr2);
console.log(arr2); // [ 1, 2, 3, 4 ], expect [2, 3, 4, 5]
If you want to modify the array, then you actually have to modify the array. You can't just write a reference to a different array over the variable (as that just throws away the local reference to that array).
function f3(arr) {
arr.length = 0; // Empty the array
f1().forEach(function (currentValue) { arr.push(currentValue); });
}
quote: "console.log(arr2); // [ 1, 2, 3, 4 ], expect [2, 3, 4, 5]"
The reason that you are not getting what you are expecting is this part here
function f3(*arr*) { *arr* = f1(); }
You are assigning the array [2,3,4,5] to the argument-name arr of the function f3, not to the arr2. Arr2 remains of course untouched and in its original state throughout your script.
function f3(*arr*) { *arr2* = f1(); } will do it.
But this answer is not my final. This is only how it appears.
You could do it in a single step:
Array.prototype.splice.apply(arr, [0, arr.length].concat(f1()));
var arr1 = [1, 2, 3, 4];
var arr2 = [1, 2, 3, 4];
function f1() {
return [2, 3, 4, 5];
}
function f2(arr) {
arr.push(5);
}
function f3(arr) {
Array.prototype.splice.apply(arr, [0, arr.length].concat(f1()));
}
f2(arr1);
document.write('<pre>' + JSON.stringify(arr1, 0, 4) + '</pre>');
f3(arr2);
document.write('<pre>' + JSON.stringify(arr2, 0, 4) + '</pre>');
When you pass anything (Whether that be an object or a primitive), all javascript does is assign a new variable while inside the function... just like using the equal sign (=)
How that parameter behaves inside the function is exactly the same as it would behave if you just assigned a new variable using the equal sign.. Take these simple examples.
You can ref: Link

Remove all elements from the initial array that are of the same value as the arguments followed by the initial array

There is an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.
Here is my code, but I am unable to solve the problem.
function destroyer(arr) {
// First I have converted the whole input into an array including the arguments
var args = Array.prototype.slice.call(arguments);
var abc=args.splice(0,1);//Here I have taken out the array part i.e[1,2,3,1,2,3] and also now args contain 2,3 only
function des(abc){
for(var i=0;i<abc.length;i++){//I tried to check it for whole length of abc array
if(args.indexOf(abc)===-1){
return true; //This should return elements of [1,2,3,1,2,3] which are not equal to 2,3 i.e [1,1] but for string inputs it is working correctly.How to do for these numbers?
}
}
}
return arr.filter(des); //but this filter function is returning empty.How to solve my problem??
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
For destroyer(["tree", "hamburger", 53], "tree", 53) the code is giving output ["hamburger"],which is working fine.
But for destroyer([1, 2, 3, 1, 2, 3], 2, 3); code is giving no output.
You can use Array.filter. Following example depicts the same
Also destroyer([1, 2, 3, 1, 2, 3], 2, 3); in this call, [1, 2, 3, 1, 2, 3] is first argument, 2 is second and 3 is third. So arr will have be [1, 2, 3, 1, 2, 3] and not [1, 2, 3, 1, 2, 3], 2, 3
function removeElementFromArray(arr,num){
var _temp = arr.filter(function(item){
return (item !== num);
});
return _temp;
}
(function main(){
var arr = [1,1,1,2,4,3,2,4,5,4,3,1,4,5,2];
var result = removeElementFromArray(arr,1);
var result1 = removeElementFromArray(arr,3);
console.log(result, result1);
})()
Try this:
function destroyer(arr) {
var args = Array.prototype.slice.call(arguments, 1);
function des(abc){
return args.indexOf(abc) === -1;
}
return arr.filter(des);
}
var result = destroyer([1, 2, 3, 1, 2, 3], 2, 3);
document.getElementById('output').innerHTML = JSON.stringify(result);
<pre id="output"></pre>
Lodash has without method that can be useful.
link to doc.
From lodash docs:
_.without(array, [values])
Creates an array excluding all provided values using SameValueZero for
equality comparisons.
Example:
_.without([1, 2, 1, 3], 1, 2);
// → [3]
This should work for you:
function destroyer(arr) {
var args = Array.prototype.slice.call(arguments);
var abc = args.splice(0, 1);
function des(value) {
// you don't need loop here, just check if given value is in args array
return args.indexOf(value) === -1;
}
// splice returns array of deleted elements so that your first argument is abc[0]
return abc[0].filter(des);
}
var result = destroyer([1, 2, 3, 1, 2, 3], 2, 3);
console.log(result);
Try this easy code:
function destroyer(arr) {
/* Put all arguments in an array using spread operator and remove elements
starting from 1 */
const args = [...arguments].splice(1);
/* Check whether arguments include elements from an array and return all that
do not include(false) */
return arr.filter(el => !args.includes(el));
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3); // [1, 1]

Categories