This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 1 year ago.
I have the following state change operation:
const handleQuantity=(e)=>{
const targetedProductId=e.target.id
// console.log(targetedProductId)
const targetedProductQuantity=e.target.value
setOrderProductQuantity({
...orderProductQuantity,
targetedProductId:targetedProductQuantity
})
}
and my state is this :
const [orderProductQuantity,setOrderProductQuantity]=React.useState({})
The
console.log(targetedProductId)
shows the correct value
It always turns out that orderProductQuantity is updated with something like this:
{"targetedProductId":e.target.value} instead of {e.target.id:e.target.value}. Could you please explain why this happens, how could I fix it ? Thank you !
P.S. Tried with string literals, also doesn't work
The problem is that you're not attributing the id value as the signatures of the parameter.
The correct way of doing so would be:
const handleQuantity = (e) => {
const targetedProductId = e.target.id;
const targetedProductQuantity = e.target.value;
setOrderProductQuantity({
...orderProductQuantity,
[targetedProductId]: targetedProductQuantity
});
}
With the square brackets you're telling javascript that you want to map the value in targetedProductId, instead of actually writing targetedProductId
Related
This question already has answers here:
Get array of object's keys
(8 answers)
Closed 3 months ago.
Object Im have Object , but how can get keys + value in this (main : , weather : , clouds: , cors : , ... )
Im try use map but can't go next entry
My demo
Sorry im bad english . thanks alot
Please, see here. This is how you get both the key and value of an object.
const foobar = {main: "hello", clouds = "world", cors: 1}
const keypairs = Object.entries(foobar);
//looping
keypairs.map(([key, value]) => { // do something });
just replace value with value.main/ value.weather and it will work
andd one thing cities.map() is not enough you dont need cities.list.map()
good luck!
This question already has answers here:
How might I extract the property values of a JavaScript object into an array?
(14 answers)
Closed 1 year ago.
I am trying to filter out a group of items from the props based on some prop. To be more specific, the mode prop. However, it's not filtering out the items, and I am getting error something like configs.filter is not a function.
I don't see anything wrong with the way I'm doing the filter here. I am trying to remove the items in the props that contain a mode prop of null.
botConfigs is not an array but it is an object that has numbers as it's properties.
So, you can do filter on Object.values:
filterConfigsByMode = (configId) => {
const { botConfigs, filterBotConfigStatus } = this.props;
const filteredList = Object.values(botConfigs).filter(item => item.mode !== null);
return filteredList;
}
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 3 years ago.
I have some url like this
/posts/singlepost?page=99
I need to get key and value of query in url and to put in new array, that i can loop,and use that key and value in my http request. In url page can be something else, i dont know what can be
You can do something like that:
// Get params.
const params = Array.from(new URLSearchParams(location.search))
// Add a loop.
params.forEach(([key, value]) => /* key is 'page' and value is 99 */)
You can use split() by ? and then split() the second element by =
let str = '/posts/singlepost?page=99';
let res = str.split('?')[1].split('=');
console.log(res)
This question already has answers here:
Object property name as number
(6 answers)
Closed 4 years ago.
I add This code to my page and I want to complete city when user tap a zipcode
and my alert dosen't show
var obj = {
"01400": "ABERGEMENT-CLÉMENCIAT",
"01640": "ABERGEMENT-DE-VAREY",
"01500": "AMBÉRIEU-EN-BUGEY",
"01330": "AMBÉRIEUX-EN-DOMBES",
"01300": "AMBLÉON",
"01500": "AMBRONAY",
"01500": "AMBUTRIX",
"01300": "ANDERT-ET-CONDON",
"01350": "ANGLEFORT",
"01100": "APREMONT",
"01110": "ARANC",
"01230": "ARANDAS",
"01100": "ARBENT",
"01300": "ARBIGNIEU",
"01190": "ARBIGNY"
};
var myVariable = obj .01400;
alert(myVariable);
Firstly, there is no key named 97433 in your object
Secondly, even if there was you cannot use property accesors with object keys which begin with a number. You need to use bracket notation.
Lastly, use console.log() for debugging as alert() coerces types and blocks the UI logic.
var myVariable = obj['97433'];
console.log(myVariable);
This question already has answers here:
Expected to return a value in arrow; function array-callback-return. Why?
(7 answers)
Closed 4 years ago.
static getDerivedStateFromProps(nextProps, prevState) {
const { claimList, claimOpenList } = nextProps
const claimsNew = [...claimList]
console.log('getDerivedStateFromProps > claimsList > ', claimList)
claimOpenList.map(item => {
const value = claimList.find(val => String(val.claimid) === String(item.claimid))
if (value === undefined) claimsNew.push(item)
})
return {
claims: claimsNew,
}
}
it works and do what is suppoused to do, but i want to get rid of this warning "Expected to return a value at the end of arrow function array-callback-return" it says the problem is on the line claimOpenList.map(item => { i really want to learn how to solve this warnings since its not the only place that it appears,i belive that understanding this one im going to be able to figure out the other ones, thanks for your time and help
i have looked another similar problems on stack but they dont seems to be the same as mine
map() is used to create a new array based on iterating another array. You are only using it to loop over an existing array.
Just change map() to forEach() which is intended for that purpose