This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 4 years ago.
I have an existing codebase that uses AngularJS, Redux and Underscore. In that codebase I have the following code:
const selectedBroker = _.findWhere(state.brokers, { brokerId: action.payload });
return state.merge({
selectedBroker,
selectedBrokerId: action.payload,
});
I want to convert it to an es6 Method. I think the find(), would be suitable, but don't know how. Could you help me a little bit? Thanks!!
Also it is not in the scope of this task but I see more underscore methods here. Like _.reject, _.contains, _.map. Could you also convert that in the context similar to the above code example.
It will be just like below, array.find will return the first matching element in the array.
let selectedBroker = state.brokers.find(broker => broker.brokerId == action.payload);
Related
This question already has answers here:
javascript es6 array feature [...data, 0] "spread operator"
(2 answers)
Closed 5 months ago.
I am a beginner at React learning it from an online tutorial.
there is an element used in one of the courses which is ... and can be seen in the following code
handleIncrement = (productId)=>{
const newProducts=[...this.state.products]
const index= newProducts.findIndex(p=> p.id === productId)
newProducts[index].count +=1 ;
this.setState({products : newProducts});
}
its usage seems to be creating an array from products and passing it to the newProducts which is an array now. could you please explain what exactly do ... and when we need it to use?
It's called the spread syntax.
In react, you're not supposed to mutate the state directly. So, if you have an array you want to edit, you will first create a copy of the array with the spread syntax, edit the new array, and finally set the state to the edited copy of the array.
Creating a copy with newProducts = this.state.products is not sufficient in this case as both the variables are pointing to the same object in memory and changing newProducts is the same as changing `this.state.products.
This question already has answers here:
How to create generic method in Go? (method must have no type parameters)
(2 answers)
Closed 11 months ago.
I would like do something like this inline function in go, and don't want to write a for loop...
const userIds = Users.map(u => u.Id);
I suggest using a package named go-funk to manupulate array/slice.
It may look like lodash in some aspects
Your code may look like this:
userIds := funk.Map(Users, func(u structTypeOfUser) int {
return u.Id
}).([]int);
It supports many other familiar functions like find, reduce, filter, contains(include)...
Repository of that package:
https://github.com/thoas/go-funk
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]();
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"
}
}
]
},
...
This question already has answers here:
What is the best-practice casing style for javascript? Why?
(6 answers)
Closed 3 years ago.
I am creating calling a function and passing in an array of objects but i am unsure if to use camingCasing or PascalCasing. Here is my method
util.load({
DefaultText:'Empty',
Items:[
{
Id:0,
Title:'Press'
}
]
});
If you notice i am passing in DefaulText, but should it be defaultText? and also Items, should it be items? and within the Items and i am also passing in Id and Title.
Can anyone confirm the correct way of doing this?
I know that methods are camelCasing but passing in objects like above?
Thanks in advance
The very popular JavaScript convention is to use PascalCasing as you call it for constructors (classes), for example String, Number, Date and camel casing for variable names and object keys. This code convention is used for all the built-in JavaScript functionality in the modern browsers, so thats why I would recommend to use it for your own code too.
There is no one correct way.
The JavaScript API uses camelCase for functions and PascalCase for objects.
Just choose one and be consistent. JavaScript identifiers are case sensitive.