How to pass array of arrays as a series of variables? [duplicate] - javascript

This question already has answers here:
Python-like unpacking in JavaScript
(3 answers)
Closed 7 years ago.
I have an array of arrays:
[
[1,2],
[2,3],
[4,3]
]
I want to pass them to a underscore-function as a series of variables.
_.intersection([1,2],[2,3],[4,3])
How can this be done?

You can use apply
_.intersection.apply(_, myArr);
The first parameter to the apply method is the this context to be set on the called method.
The second parameter is an array whose elements will be passed as individual arguments to the called method.
MDN Docs
The apply() method calls a function with a given this value and arguments provided as an array (or an array-like object).

Related

indexOf not working correctly in javaScript [duplicate]

This question already has answers here:
Difference Between indexOf and findIndex function of array
(9 answers)
Closed 4 months ago.
my code is :
[{a:1},{b:2},{c:3}].indexOf(obj=>{return obj.a ==1})
I expect return 0 but result is -1
what is the problem?
The main difference are the parameters of these functions:
Array.prototype.indexOf() expects a value as first parameter. This
makes it a good choice to find the index in arrays of primitive types
(like string, number, or boolean).
Array.prototype.findIndex() expects a callback as first parameter.
Use this if you need the index in arrays with non-primitive types
(e.g. objects) or your find condition is more complex than just a
value.
This question is similar to this and the answer can also be found there.

Pass to method object values as arguments [duplicate]

This question already has answers here:
Passing an array as a function parameter in JavaScript
(12 answers)
Closed 2 years ago.
I have next code:
someFunct(Object.values(Obj));
What I need is to pass object values in to 'someFunct' as separate argument, not as array.
You can use the spreading operator to spread the values:
someFunct(...Object.values(Obj));
You should use spread operator ...
someFunct(...Object.values(Obj));
Note: This method may cause some problems in code because Object.values() is not guaranteed to have same order. The better option is to pass and object and then destructure it.

Can you tell me why we use second argument is JSON.stringify? [duplicate]

This question already has answers here:
JSON.Stringfy method's replacer argument not working on nested object
(2 answers)
JSON.stringify filter for properties with object values is not working
(1 answer)
Closed 3 years ago.
Can anyone tell me why we use second argument i.e Replacer in JSON.Stringify() like:
var a = {
b: 42,
c: "42",
d: [1,2,3]
};
JSON.stringify( a, ["b","c"] );
It serves as a whitelist for the JSON stringify function, as stated in MDN:
A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string.

What's an easiest way to make sure my key is always first in a javascript object? [duplicate]

This question already has answers here:
Does ES6 introduce a well-defined order of enumeration for object properties?
(3 answers)
Does JavaScript guarantee object property order?
(13 answers)
Closed 5 years ago.
I have an object that varies in values and order of keys.
var obj = { extension50: {}, extension55: {}, main: {} }
Note I have "main" key placed out of order. It should be placed in the beginning so when someone iterates through keys of this object it would come up first.
What's the best way of achieving it?
JavaScript objects are not ordered. If you want a specific order, use arrays.

How do arguments work in Javascript LIteral Functions? [duplicate]

This question already has answers here:
Parameters inside callback function in Javascript
(1 answer)
Higher-Order Functions in JS
(1 answer)
Closed 2 years ago.
I have this code that filters an array using the .filter method.
I'm extremely confused about the function's parameters that is being specified in the .filter method.
Where does the parameter come from? How do I know when to add a parameter like 'value', and what is the value of the parameter 'value'?
var newArray = [1,2,3,4,5,6,7,8,9,10];
newArray = newArray.filter(function(value) {
return value < 6;
});
I'm not too sure if it is the right term to use.
Where does the parameter come from?
It gets passed to the function when the function is called … which will happen somewhere inside filter or a function called by filter.
How do I know when to add a parameter like 'value',
Generally by reading the documentation
what is the value of the parameter 'value'
From the documentation, the first argument is:
element:
The current element being processed in the array.
Array is a JavaScript class and each JavaScript class can have prototype. Filter method is a part of Array prototype and this function is getting as an argument callback function with one argument.
Inside Filter function, your callback is being called on each array item.
You can find more informations about this function in JavaScript docs.

Categories

Resources