Pass to method object values as arguments [duplicate] - javascript

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.

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.

Value of function parameter is not getting [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 3 years ago.
I'm not getting the actual value of my parameter name in the below function. Object.assign taking the function parameter name as a string literal so the resulted JSON object also named as name
see the below code.
Please see the resulted json object i got.
How to fix this?
wrap it with []
return Object.assign(..., {[name]: JSON.parse(...)})

How to make string to object or array format [duplicate]

This question already has answers here:
Access a nested property with a string [duplicate]
(5 answers)
Get global variable dynamically by name string in JavaScript
(6 answers)
Closed 4 years ago.
I have to create a string in format like below
"currentState[0]['children'][1]"
But I need to execute it later just like below
currentState[0]['children'][1]
I have elements and childrens on currentState. But while looping I have to create a string. But later I need to execute as array.
I have tried almost all array methods. Array.call, bind etc. And string methods as well. Could not get the output
How can I make it
Please be more specific with your question but from my understanding, you can use javascript's eval() function to execute a string as javascript, so when you need to execute it, just run eval("currentState[0]['children'][1]").
The alternative to the eval() would be
function evalFn(obj) {
return Function('"use strict";return (' + obj + ')')();
}
evalFn("currentState[0]['children'][1]")
refer to: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval for a more in-depth explanation.

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 to pass array of arrays as a series of variables? [duplicate]

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).

Categories

Resources