ReactJS : How can get value in object [duplicate] - javascript

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!

Related

Javascript treats variable as string [duplicate]

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

Get "id" part of outlook emai [duplicate]

This question already has answers here:
JavaScript - Get Portion of URL Path
(6 answers)
Closed 1 year ago.
Just a quick question, how would you go about getting the "id" from this URL:
https://outlook.live.com/mail/0/inbox/id/AQQkADAwATM0MDAAMS0xZGUwLTNjMTAtMDACLTAwCgAQAB%2FnQ1lgT6dDlqIakp3j4qk%3D
These URLs change alot, but i just want the message id.
Its not just a query param, so i can't just go and get it from there.
How would i do this?
You can just split the URL by / and get the 8th index, and if there is parameters, just split it by ? and get the first index
const url =
'https://outlook.live.com/mail/0/inbox/id/AQQkADAwATM0MDAAMS0xZGUwLTNjMTAtMDACLTAwCgAQAB%2FnQ1lgT6dDlqIakp3j4qk%3D?a=1&'
let url_split = url.split('/')
let id_with_parameter = url_split[7]
let id = id_with_parameter.split('?')[0]
console.log(id)
Output AQQkADAwATM0MDAAMS0xZGUwLTNjMTAtMDACLTAwCgAQAB%2FnQ1lgT6dDlqIakp3j4qk%3D
This probably has a lot of limitation and not the best way

JSON object to Array Javascript [duplicate]

This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 2 years ago.
Im seeing a lot of answers to this in simple arrays but I need the keys and value. I have a json object but need an array. The object looks like this:
{"Food":"Starbucks", "Job":"Electrician"}
I just need a simple array like this:
["Food" => "Starbucks", "Job" => "Electrician"]
Edit: Im not sure how to type it out. Just need to be able to do an array.map and get the keys and values in javascript.
Here is the final code that I am trying:
const details = jsonObject;
{details.map(function(item,idx){
return<DetailCell>
<Label>{idx}</Label>
<Text style={TextStyle}>{item}</Text>
</DetailCell>
})}
Label should be the key and the Text should be the value.
I think you need this :
[{"Food" : "Starbucks"}, {"Job" : "Electrician"}]
To declare a literal array value is something like this in JSON in node.
{JSONVarWithArrayValue : [0,0,0,0]}

Assign dynamic index in array of object in Angularjs [duplicate]

This question already has answers here:
Variable as the property name in a JavaScript object literal? [duplicate]
(3 answers)
Closed 6 years ago.
If I have array of object in such a way.
data[0].name= 'Prashant Shukla';
$scope.getColumn[i].Field = 'name';
Using above I want to assign Prashant in $scope.getColumn[i].value in dynamic way. I have try like this
$scope.getColumn[i].value = data[0].+$scope.getColumn[i].Field;
but this give me Uncaught SyntaxError: Unexpected token +
I can get Prashant Shukla by data[0].name but How can I get exactly same by using $scope.getColumn[i].Field in place of name index ?
how can I resolve this error and assign value dynamically?
thanks ahead.
you can use as like as given below:
data[0].name= 'Prashant Shukla';
$scope.getColumn[i].Field = 'name';
console.log( data[0][ $scope.getColumn[i].Field ] ); // 'Prashant Shukla'
$scope.getColumn[i].value = data[0].+$scope.getColumn[i].Field;
Why you have . after the data[0]?
It should be data[0].name +$scope.getColumn[i].Field;

Get key from JSON object [duplicate]

This question already has answers here:
Javascript get object key name
(8 answers)
Closed 5 years ago.
I'm using the flat-cache NPM package and I'm currently blocked because I can't recover the key from the data I'm caching. It must be very simple, but I'm starting with the JS and I'm pulling my hair out on this problem.
Simple example of code :
main.js
var flatCache = require('flat-cache')
flatCache.setKey('d86f003c-bf0a-4b08-9744-1081c78ece9d', {"creation":"2018/02/20", "link":"https://www.npmjs.com/package/uuid","comment":"UUID", "tags":["NPM", "UUID"]});
var a = flatCache.all();
console.log(a);
Example of data from console :
{
"d86f003c-bf0a-4b08-9744-1081c78ece9d": {
"date":"20180220",
"comment":"Hello world",
"tags":[
"hello",
"worlds"
]
}
}
What would be the procedure to follow to retrieve the key : d86f003c-bf0a-4b08-9744-1081c78ece9d ?
Thank you in advance for your answer !
Use Object.keys method.
In your case:
// `a` is defined somewhere there
...
Object.keys(a); // an array of object keys - but only the first level
console.log(Object.keys(a)[0]); // should log `d86f003c-bf0a-4b08-9744-1081c78ece9d`
for further reference - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

Categories

Resources