How sort this json format in react native using map function - reactjs

I have the following JSON format
{
"name":"xyz",
"age-group":"bb"
}
How to get sort out this JSON
I use the following code
const array = [{ "name":"xyz", "age-group":"bb"} ]
array.map((element) => {
Console.log(element.age-group)
});
I got an error:
Can't find variable: group

1st thing is you can't map object.
and in your case you set json object in array and then you are maping it.
so it must be like:
array[0].name;
but in second property, it has hyphen which looks like "age-group" to access object property containing hyphen is:
array[0].obj["age-group"] //this is return `bb`
so in your code it will be:
array.map((element) => {
console.log(element.obj["age-group"])
});
to access object property with hyphen(-) you need to use notation with square brackets.

Related

Access object properties React-Native

I have a response from the server that looks like this
[
{
"metric":{
"id":"b66178b8-dc18-11e8-9f8b-f2801f1b9fd1",
"name":"Detector de fum bucatarie",
"metricValueType":"DOUBLE",
"metricType":"DEVICE_VALUE",
"groupUUID":null,
"projectUUID":null,
"companyUUID":"ccab28ed-3dcf-411f-8748-ec7740ae559c",
"unit":null,
"formula":null
},
"data":{
"1550150771819":"10.310835857351371"
}
}
]
Data property contains a hashMap with Timestamp and a Value.
When I try to get any value I recive this error:
myErrorTypeError: undefined is not an object (evaluating 'metricValues.metric.id')
How can I access a property ? I tried both methods :
metricValues.metric.id
and
metricValues["metric"]["id"]
How can I get the hashMap values?
I already tried this :
const timestamp = Object.keys(metricValues.data)[0];
const values = Object.values(metricValues.data)[0];
Your data is an array of objects. So even though your data has only one object, still it is an array of objects.
your object is at index 0 of the array.
You can access the metric Id as follows,
metricValues[0].metric.id
Try This (put that [0])
metricValues[0].metric.id
Your response from the server is an array. And the metric object is in the first element of that array.
To access the id inside the metric object you need to pass the index at which that object is present in the array which is 0 here.
That's why use :
metricValues[0].metric.id

How to map new property values to an array of JSON objects?

I'm reading back record sets in an express server using the node mssql package. Reading back the values outputs an array of Json objects as expected.
Now I need to modify the Email propoerty value of each Json object. So I tried looping through the recordset and changing the value at each index:
var request = new sql.Request(sql.globalConnection);
request.input('p_email', sql.VarChar(50), userEmail);
request.execute('GetDDMUserProfile', function(err, recordsets, returnValue) {
for (var i = 0; i < recordsets.length; i++){
recordsets[i].Email = "joe#gmail.com";
}
console.log(recordsets);
});
But instead of modifying the Emailvalue of each Json object, this code just appends a new email property to the last Json object.
How can you map new property values to an array of JSON objects?
Example output:
An example of the output is shown below, where a new Email property has been added to the end of the array instead of changing each existing property value:
[
[
{
ID:[
4
],
UserName:"Brian",
Email:"joe#gmail.com"
},
{
ID:[
5
],
UserName:"Brian",
Email:"joe#gmail.com"
}
Email:'joe#gmail.com' ]
]
The issue here is that your dataset appears to not be an array of JSON objects but, rather, an array of arrays of JSON objects. If you know for certain that you'll always have only one array in the top-most array, then you can solve the problem like this:
recordsets[0][i].Email = "joe#gmail.com";
to always target the first array in the top-most array. However, if the top-most array could potentially have more than one array, that'll be a different kind of issue to solve.

React JS array forEach get refs value using variable

How can i get this to work correctly using this method below? I need to go through each value in the array
array.forEach((v) => {
let a = `this.refs.a${v}.value`
console.log(a) // prints this.refs.a0.value
console.log(this.refs.a0.value) // prints correct value
})
I assume you are trying to access refs by name. In JavaScript you can access object's property using object[propertyName] syntax, where the property name is a string.
Using this method you can rewrite your property access to this.refs[`a${v}`].value.
So the final code should look like this:
array.forEach((v) => {
let r = this.refs[`a${v}`].value
console.log(r) // prints correct value
})

Value from Object using node

The below is my data, its type is object, I am not able to get my data after stringifying and parsing too...how can I get message alone using nodejs.
[ { ID: '361',Message: 'customg' } ]
I guess what you want to say is this. You got an array of object(s) like this:
var myarray =[ { ID: '361',Message: 'customg' } ] ;
stringifying the json object using JSON.stringify(myarray), gives :
"[{\"ID\":\"361\",\"Message\":\"customg\"}]"
parsing back using JSON.parse ("[{\"ID\":\"361\",\"Message\":\"customg\"}]"), returns your original object.
Now to access the 'Message' member, you need to access the first item in the array, then the Message property of the object, like this:
var msg =myarray [0].Message ;
This code assumes, you got an object with a property Message as the first element of the array.
Your data is,
data = [ { ID: '361',Message: 'customg' } ]
data variable contains list of Objects(Here there is only one object in list).
var msg = data[0].Message // You will get message property
In your case you already have list of objects, so no need to stringify/parse the list.
JSON.stringify() : used when you want to convert your JSON object to string.
JSON.parse() : used when you want JSON object from string.(given that string contains proper JSON object)

How do i get the specific value from json data using Angularjs

i am new to AngularJs. I am trying to get the values from json based on search key word and displaying that related data.
Below is my json format
{"abc":[
{ "url":"abc.json"}
],
"bbc":[
{"url":"bbc.json"}
]
}
i am comparing the key with entered search word. If it matches then i need to load the separate json which is related to found key. I am able to compare the search word with key, but when i am trying to get the value am getting total value like url=abc.json, instead of this i've to get only abc.json. My controller code is as follows
function getDetailsController($scope,$http) {
$scope.search = function() {
$http.get("searchInfo.json").success(function(response) {
angular.forEach(response, function(value, key) {
if(angular.equals(key, $scope.searchText)){
$scope.url = value;
$http.get(url).success(function(response) {
$scope.details = response;
});
}
});
});
};
}
I have tried in diffrent ways but i was not able to get the value. Could you please help me on this.
In your JSON, '"abc"': is a key, and its corresponding value is
[{ "url":"abc.json"}]
That is an array containing a single object, the object containing a single key: "url".
So, to access this URL, you simply need
value[0].url
This structure is quite strange though. I don't really understand the point of wrapping the object inside an array.

Resources