Javascript array of object [closed] - javascript

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
I want to have an array containing an object. For example I have an object like this:
parentObj:{
childObj1,
childObj2
}
but I want to have a "parentObj" array and be able to call it like :
parentObj[0].childObj1
I searched a lot but didn't find anything related to this. I'll appreciate if someone can help me with this.
thanks very much

Here's an array of objects:
var a = [
{name: "first"},
{name: "second"}
];
console.log(a[0].name); // "first"
console.log(a[1].name); // "second"
The [] is an array initializer. The {} is an object initializer.

Related

Store user input in an array or object [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I would like to know whether it is easier to store all the data from a text input where the user has to enter their surname in an array or in an object. And how do I do so?.
Thanks in advance
You could do something like this:
var dataArray = [];
function storeData() {
var data = document.getElementById("yourInput").value;
dataArray.push(data);
}
This method uses arrays - personally I find them more easier to use than objects.

Access values from JSON in NodeJS [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a function that will take in a JSON object which has a similar structure as follows:-
{
"listing": [
{
"rental_prices": {
"shared_occupancy": "N",
"per_week": 2308,
"accurate": "per_month",
"per_month": 10000
},
"country_code": "gb",
"num_floors": "0",
}
]
}
I want to be able to get all the values for per_month from each sub object of listing I can pull a specific one, i.e. using data.listing[0].rental_prices.per_month but I want to be able to iterate over the object and get the value and push it to a new array each time it comes aross the per_month key
Indeed as #mwilson suggested .map would do the trick in this case.
https://repl.it/repls/TriangularModestNormalform
testJson.listing.map(listing => listing.rental_prices.per_month);
Note: This will return an array with undefined items if per_month doesn't exist, and will also crash if renal_prices doesn't exist

How to parse or loop this [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 5 years ago.
Improve this question
I have tried everything I can think of to get the data but cannot figure this out.
The data is in a structure like this:
[{"code":1000,"day":"Sunny","night":"Clear","icon":113,"languages":
[{"lang_name":"Arabic","lang_iso":"ar","day_text":"مشمس","night_text":"صافي"}]
}]
I've tried looping, using key:value using the dot notation and bracket notation and cannot get the info.
I'm trying to get to the "languages" so I can parse them for the weather.
var arr = [{"code":1000,"day":"Sunny","night":"Clear","icon":113,"languages":
[{"lang_name":"Arabic","lang_iso":"ar","day_text":"مشمس","night_text":"صافي"}]
}]
arr.forEach(function(obj){ //loop array
obj.languages.forEach(function(language) { //loop languages
console.log(language); //language object
console.log(language.lang_name); //language property
});
});

How to deep clone this part of javascript? [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 6 years ago.
Improve this question
still very new in js overall and i'm trying to deep clone this part of code.
$scope.add = function() {
$scope.data.push(Object.assign(mock));
};
If anyone could help me cloning this, would be grateful. Thanks
use angular's .copy() method to clone.
like this
$scope.data.push(angular.copy(mock));
If you want to create an actual clone where changes to the clone don't effect the object you are cloning:
Object.assign({}, mock)
If you are using jQuery, another way to deep copy is by using .extend(true, target, object1 [, objectN ] )
https://api.jquery.com/jquery.extend/

How do I get length of my Json Object [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
Below is my JSON object
{"Decision":[{"recid":"1183","reason":"Approved as Requested","decision":"Approved","approvalamt":"","comment":""},{"recid":"662","reason":"3","decision":"Rejected","approvalamt":"","comment":""},{"recid":"752","reason":"Approved People Resources; But No Funding Approved","decision":"Approved","approvalamt":"","comment":""}]}
How do I get length of this.In this example it should return me 3.
My Object name is DecisionObj which have above listed elements.
var string = '{"Decision":[{"recid":"1183","reason":"Approved as Requested","decision":"Approved","approvalamt":"","comment":""},{"recid":"662","reason":"3","decision":"Rejected","approvalamt":"","comment":""},{"recid":"752","reason":"Approved People Resources; But No Funding Approved","decision":"Approved","approvalamt":"","comment":""}]}';
var object = JSON.parse(string);
alert(object.Decision.length);
Arrays have length property. You can use that.
var length = DecisionObj.length;

Categories