Javascript how to properly access associative array elements [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am having a bit of difficulty understanding how to access elements from a 2 dimension associative array. For example, when I try using the "price" key from the first array, it returns as object Object. I am having a hard time returning the values that the keys are related to. I have tried without quotations also. Thanks for your time.
var houseData = Array(
{
"price": "$320, 000",
"imgsrc": "./images/colonial.jpg",
"description": "Colonial House, 3 bedrooms"
},
{
"price": "$459, 999",
"imgsrc": "./images/contemporary.jpg",
"description": "Colonial House, 3 bedrooms"
},
{
"price": "$179, 500",
"imgsrc": "./images/cottage.jpg",
"description": "Colonial House, 3 bedrooms"
},
{
"price": "$210, 000",
"imgsrc": "./images/ranch.jpg",
"description": "Colonial House, 3 bedrooms"
},
{
"price": "$159, 999",
"imgsrc": "./images/townhouse.jpg",
"description": "Colonial House, 3 bedrooms"
});

Basically, brackets in JS will create an object. With the Array function, you are creating a list of object. So as #Salehen Rahman said, you need to first access the object in the list by specifying an index :
var objectAtIndex0 = houseData[0];
and then access the data in the object:
var priceFromObjectAtIndex0 = objectAtIndex0.price;
or one-line:
var priceFromObjectAtIndex0 = houseData[0].price;

Related

Check is JSON is valid [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm trying to get JSON in a valid format so that it can be sent to an API and I can't figure out why the below JSON is not valid. Please can someone explain why this is not valid?
{
"Description": "test",
"Quantity": "0.30",
"UnitAmount": "6400.0",
"TaxType": "OUTPUT2",
"AccountCode": "200"
},
{
"Description": "test2",
"Quantity": "0.30",
"UnitAmount": "0.0",
"TaxType": "OUTPUT2",
"AccountCode": "200"
}
The top level of a JSON text must be one of the JSON data types (like object, array, or string).
There can only be one data type at the top level.
You have an object but then you have a comma and then a second object.
Perhaps you should wrap it in an array.

Meteor/mongo get records created one month ago [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How do I get records from my collection created one month ago with moment.js return a count of all the records.
My collection code is:
Products.find() // where start and end date is one month ago and products is the collection
This might be a silly question but, as I am coming from rails to javascript/meteor, a little help will be fine.
sample document
{
"_id": "xxx",
"name": "xxx",
"description": "xxx",
"createdAt": "2015-04-18T04:40:00Z",
"productType": "Flavoured Milk",
"opt1": "Packaging",
"opt2": "Weight",
"supplier": "xxx",
"brand": "xxx"
"status": "active",
"tags": ["xxx","xxx"],
"updatedAt": "2015-04-18T04:40:00Z"
}
You can get the Date object of one month ago by:
moment().subtract(1, "months").toDate();
I assume that you have field createdAt on Products collection. So the query to find all products created on month ago will be:
var oneMonthAgo = moment().subtract('months', 1).toDate();
Products.find({ createdAt: oneMonthAgo })
But keep in mind that the solution above will return only records that created at exactly one month ago accurate to second.
Uchenna,
In Moment.js 2.8.0 you could do something as such:
var comparison = moment().subtract(1, 'months').toDate();
Products.find({ createdAt: comparison });
According to the docs:
Before version 2.8.0, the moment#subtract(String, Number) syntax was
also supported. It has been deprecated in favor of
moment#subtract(Number, String).

get data from multidimensional json object [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to get the result of the following json object returned by google driving api.
"routes": [
{
"bounds": {
"Ga": {
"C": 39.01115,
"j": 39.06483
},
"xa": {
"j": -77.57857000000001,
"C": -77.47601
}
},
"copyrights": "Map data ©2015 Google",
"legs": [
{
"distance": {
"text": "9.4 mi",
"value": 15096
},
"duration": {
"text": "15 mins",
"value": 870
},
"end_address":"1234 Any St USA".....//and so on
how would i go about getting the data in "text" or "end_address"
i have tried
alert(result.routes.legs.distance.text)
and
alert(result.routes.legs.end_address);
and i get
Uncaught TypeError: Cannot read property 'distance' of undefined
and
Uncaught TypeError: Cannot read property 'end_address' of undefined
respectively
routes is an array and legs is an array within each of the routes array objects
Try
alert(routes[0].legs[0].distance.text)

Id increment front-end logic [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
$scope.items.push({
"itemId": $scope.tabId + 1,
"itemName" : itemName,
});
I console.log($scope.itemId) every time it get pushed but it doesn't increase.
It could be done to use $http after every pushes but it's good to keep the server light. If for a unque field you don't use auto increment in backend, how would u handle this issue? I mean the best practice.
Your code will always push the same ID (as you recognized) because you never assign the incremented value to $scope.tabId.
Change it to something like this:
function yourFunction() {
// increment $scope.tabId
$scope.tabId = $scope.tabId + 1
// push the new element to your array
$scope.items.push({
"itemId": $scope.tabId,
"itemName": itemName
});
}

How to convert object array into 2d array of parent child hierarchy? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Using this declaration for multidimensional object (shown below) can I make attacksource of label type "inside" to contain multiple children e.g "dos","login abuse"
var data = [
{ "attacksource": 43, "attacktype": 60, "AT":"DoS","Label": "iNISDE" },
{ "attacksource": 29, "attacktype": 40, "AT":"login abuse","Label": "outside" }
];
What I have tried using js is as
var data =
[
[{"attacksource": 43,"Label":"Inside"}],
[
{"attacktype": 13,"Label":"dos"},
{"attacktype": 13,"Label":"virus"}
]
];
Is this a correct assignment for this task?
Thanks
What you want is JSON. It's not clear whether you're talking about transforming an existing JSON or building one from scratch.
It's hard to tell you the best way to represent your data without seeing the complete picture. But if a single attack source can have multiple attacks types then one way would be to represent those attack types as an array.
[
{
"attacksource": 43,
"label": "inside",
"attackTypes": [
{"attacktype": 60, "AT":"DoS"},
{"attacktype": 18, "AT":"virus"}
]
}
]

Categories