get amount of properties defined in state object react native [duplicate] - javascript

This question already has answers here:
How to efficiently count the number of keys/properties of an object in JavaScript
(19 answers)
Closed 3 years ago.
I have an data object in a state, like so:
this.state = {
data: {
newsData: [],
eventData: [],
},
}
How can I get the number of properties defined in the state data? data.length doesn't work since it's not an array.

You can use Object.values(data).length

Related

How to extend variable name with a number? [duplicate]

This question already has answers here:
Getting a Custom Objects properties by string var [duplicate]
(2 answers)
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 4 months ago.
I have an array retrieved from a JSON file and I am using dataArray.forEach(function(e, idx, array) to loop every element and create a table.
I can simply call for each element with console.log(e.id) but how can I list properties with incremental numbers on them without specifying it or its exact the position in loop?
I need to end up with something like this console.log(e.dp_1.value); but instead of this number I wish to use idx. Is this possible?
I have heard there is getChildByName but not sure how to use it and if it fits.
{
"devices": [{
"id": 0,
"sn": "#12BB56CC9",
"dp_1": {
"value": "65345",
"multiplier": 0.001,
"digits": 2
},
"dp_2": {
"value": "55345",
"multiplier": 0.001,
"digits": 2
}
},
// much more like those
]
}

Add a new attribute to json object [duplicate]

This question already has answers here:
Add new attribute (element) to JSON object using JavaScript
(11 answers)
Add property to each object in the array [duplicate]
(2 answers)
Closed 6 years ago.
I have a JSON object of the following format
[{"field1":1,"field2":"A","field3":"B"},
{"field1":2,"field2":"B","field3":"C"},
{......},
{......},
]
I need to add a new attribute to each row based on some calculations.
Expected result
[{"field1":1,"field2":"A","field3":"B","field4"="generatedVal1"},
{"field1":2,"field2":"B","field3":"C","field4"="generatedVal2"},
{......},
{......},
]
How can I achieve this using javascript?
Use Array.prototype.forEach method:
[
{"field1":1,"field2":"A","field3":"B"},
{"field1":2,"field2":"B","field3":"C"}
]
.forEach(obj => {
obj.field4 = 'Something'
})
Sidenote on terminology: you don't have any JSON, you have javascript array (object). JSON is a string representation of this object, but in your question you are talking about object itself.

Finding property in Object [duplicate]

This question already has answers here:
How do I check if an object has a specific property in JavaScript?
(31 answers)
Closed 6 years ago.
Imagine this scenario:
var myObject = {
"1030":{},
"1059":{}
}
I want to check if 1030 is in that object.
How would I do this?
Try hasOwnProperty
if(myObject.hasOwnProperty("1030")) {
// Do code
}
It's a bit safer than checking if(myObject["1030"]). This will return false, if the value is falsey (false, undefined, null), which may be desirable, but also does not strictly mean it does not exist.

Accessing element by variable [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
I have an array and want to access its data. However, I need to use a variable instead of the name to access the data.
For example;
My data:
$scope.myData = {
"user": [
{ child[{......}],
..........
}
],"user2": [
{
child[{......}],
.........
}
],...........
The following works
console.log("lenght:"+$scope.myData.user[0].child.length);
but I want to use a variable instead of user[0], because it is dynamic, it changes every time.
Similar to
var m=user;
console.log("lenght:"+$scope.myData.m[0].child.length);
How about this?
var m = 'user';
console.log("lenght:"+ $scope.myData[m][0].child.length );

Accessing an array within an object [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
var oldMatches = Meteor.users.find(userId, {
fields: {
_id: false,
matches: true
}
}).fetch()
This returns an object with an empty matches array, as it should. How would I access this array to find the length of it?
Thanks.
You would find the length of the array with
oldMatches.length
So:
if(oldMatches.length) {
// is not empty
} else {
// is empty
}
N.B. length only works on a returned array from fetch, not a Meteor cursor

Categories