Javascript API syntax help - const { uport, MNID } [duplicate] - javascript

This question already has answers here:
What is the difference between const and const {} in JavaScript
(4 answers)
Closed 4 years ago.
So while I was making my react native app, I tried to use an API from
https://github.com/uport-project/react-native-uport-connect and there is a syntax that I've yet to understand.
May I know what does const { uport, MNID } mean from this code
import configureUportConnect from 'react-native-uport-connect'
const { uport, MNID } = configureUportConnect({
appName: 'uPort Demo',
appAddress: '2oeXufHGDpU51bfKBsZDdu7Je9weJ3r7sVG',
privateKey:'<PRIVATE_KEY>',
})
Im quite new to this and this code is placed on a seperate js file and im trying to export const { uport, MNID } so I could use it in my Components and im not sure if it's a variable, object or some js syntax. Thank you!

This is called destructuring, and it means you are assigning your variables, not to the object that the function returns, but to the individual properties of that object, specifically the properties at the keys uport and MNID. The alternative syntax would be to say const variableName = // etc... and then you would access the properties like: variableName.uport.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring

Related

Turn a string into function in JS [duplicate]

This question already has answers here:
How to execute a JavaScript function when I have its name as a string
(36 answers)
Closed 2 years ago.
My problem is simple and I couldn't find the proper answer in this forum. My bad...
I want to do that :
const dataReceived = foo;
foo(state);
How can I do that?
I read it is better to avoid eval, and I couldn't get success with new Function.
Thanks for your help!
EDIT
Thanks for your answers.
I work with React.
In my reducer, I have a create_item case.
I can reach action.category, that can be the word 'currency' or 'country'.
What I want to do is to launch either the method createCurrency or createCountry according what is inside action.category.
That's why I tried to join 'create' and 'action.category' to create a dynamic function name.
But it seems to be a poor idea...
The simplest approach is to create an object which contains an entry where:
the key is a string
the value is a function.
Example:
const myObject = {
myFunction: () => { [... DO SOMETHING...] }
}
Subsequently you will be able to invoke the function, using:
myObject.myFunction();
The above becomes more powerful when you use brackets notation.
Example:
const myString = 'myFunction';
myObject[myString]();

What is the difference between var {todo}= require("./models/todo"); and var todo=require("./models/todo"); [duplicate]

This question already has answers here:
Curly brackets (braces) in Node.js 'require' statement
(2 answers)
Closed 3 years ago.
I am trying to load a todo model from my todo class and I have two options. The first one is
var {todo}= require("./models/todo");
and second one is
var todo=require("./models/todo");
I am confused which is what.
The first one is a destructuring assignment. It means "take an object from "models/todo" and assign its property "todo" to my local variable "todo". If it contains no such property, you'll get undefined assigned to the variable.
For example this if is your model
module.exports = {
toLower: obj => {
},
streamIdea: async (idea) => {
}
}
if you're doing this
const model = require('mymodel');
then you have to call your functions like this,
model.toLower()
which means you're importing everything and calling it by function name
and if you're importing like this:
const { toLower } = require('mymodel');
it means you're only importing toLower from this model now you can just call it like this
toLower();
without need of model.

Creating a dumb component [duplicate]

This question already has answers here:
Proper use of const for defining functions
(5 answers)
Closed 5 years ago.
I was wondering if there is any difference in performance when dealing with dumb component in React, since there are 2 possible ways to achieve the same result.
function Comp(props) {
...
}
const Comp = props => {
...
}
Really they are two ways to define a function and there should be no difference in the performances.
There's definitely no difference between the two in your example. Hence this code also gets compiled and you end up having the same thing:
function CompA(props) {}
const CompB = props => {}
gets transpiled to:
function CompA(props) {}
var CompB = function CompB(props) {};
edit: There is difference in both functions tho. In performance they are the same but in behavior the code is different. We have hoisting and different context involved.
edit2: Well, it looks like there IS a difference. Check out https://jsperf.com/react-stateless-compare

Really basic javascript function concept [duplicate]

This question already has an answer here:
How do I access an object property dynamically using a variable in JavaScript?
(1 answer)
Closed 5 years ago.
Testing my ability to write code in JavaScript and Node (perhaps a bit of a monumental effort) and also attempting to understand standards.
I want to dynamically change an attribute in an object as in this examnple:
var parms = {
host:'',
port:'',
user:'',
pass:''
};
parms.user='foo';
parms.pass='bar';
console.log(parms.user);
setParm = function(param,value){
parms.param = value;
}
setParm('user','baz');
console.log(parms.user);
However, I'm completely blind. I feel as though I may be in a blind alley in terms of what I think is possible versus what is actually workable.
You are passing the property as a string, so accessing with . won't work. One solution I know is that you can use dict-like indexing:
var parms = {
host:'',
port:'',
user:'',
pass:''
};
parms.user='foo';
parms.pass='bar';
console.log(parms.user);
setParm = function(param,value){
parms[param] = value;
}
setParm('user','baz');
console.log(parms.user);

Better mapping JSON to objects in JavaScript [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Access Javascript nested objects safely
(14 answers)
Test for existence of nested JavaScript object key
(64 answers)
Closed 5 years ago.
I'm using v4 sheets API from google-api-nodejs-client, it returns a lot of data. Though I filter it using fields arguments, the structure of JSON is very complex. So to incorporate it into my logic, I flatten it. And it needs to be safe, I don't want any exceptions to be thrown.
This is how the UNSAFE version of the code looks:
const data = response.sheets[0].data;
const columns = data.map((column) => {
const rowData = column.rowData;
const values = rowData[0].values;
return rowData.map((cellData) => cellData.values[0].userEnteredValue);
});
// ...
If JSON is invalid, an exception is inevitable. I don't want this. The problem is that to fix it, I should add plenty of if-s. This would make the code look ugly.
Let's summarize: Is there a way to map JSON into local objects safely and expressive (and preferably relying only on features of ES6)?
UPDATE
Let's add a little more context. This is a small insight how deep the JSON is, and all I need to map it to an array of strings ('1.06' is one of them).
{
"sheets":[
{
"data":[
{
"rowData":[
{
"values":[
{
"userEnteredValue":{
"stringValue":"1.06"
}
}
]
},
...

Categories