0
var data = [
 {
 infoType: [
 [1],
 [2],
 [3],
 ]
 },
 {
 infoType: [
 [4],
 [5],
 [6],
 ] 
 },
 {
 infoType: [
 [7],
 [7],
 [8],
 ] 
 },
 {
 infoType: [
 [9],
 [10],
 [11],
 ] 
 }
]

Guys, i have this data with several objects inside, and inside this objects i have a field 'infoType', and i'm trying to print all the values, but i am not getting.

How can i do that?

I was trying like this:

for(var i = 0; i < data.length; i++) {
 data[i].infoType.forEach(function(item) {
 console.log(item);
 })
}
for(var i = 0; i < data.length; i++) {
 for(var j = 0; i < data[i].infoType.length; i++) {
 console.log(data[i].infoType[j]);
 }
}

I can'T use jquery for this.

asked Feb 19, 2015 at 10:18

5 Answers 5

1

i have done this solution before array in array. but in c# side for google maps markers

str.Append("<script type='text/javascript'>");
 str.Append("var _arrSitesLatLang = new Array();");
 StationsDataDB objSitesDB = new StationsDataDB();
 DataTable dt = objSitesDB.GetBySitesIdAndDates(Request.QueryString["SId"], Request.QueryString["date"], Request.QueryString["Q"]);
 for (int i = 0; i < dt.Rows.Count; i++)
 {
 str.Append("var arr = new Array();");
 str.Append("arr.push('" + dt.Rows[i]["Station"].ToString() + "');");
 string[] latlang = null;
 if (!string.IsNullOrEmpty(dt.Rows[i]["LatLang"].ToString()))
 {
 latlang = dt.Rows[i]["LatLang"].ToString().Trim().Split(',');
 str.Append("arr.push(" + latlang[0] + ");");
 str.Append("arr.push(" + latlang[1] + ");");
 }
 str.Append("_arrSitesLatLang.push(arr);");
 }

and in the client side

var markers = _arrSitesLatLang;
for (i = 0; i < markers.length; i++) {
 var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
 bounds.extend(position);
 marker = new google.maps.Marker({
 position: position,
 map: map,
 title: markers[i][0]
 });
 // Allow each marker to have an info window 
 google.maps.event.addListener(marker, 'click', (function (marker, i) {
 return function () {
 infoWindow.setContent(infoWindowContent[i][0]);
 infoWindow.open(map, marker);
 }
 })(marker, i));
 // Automatically center the map fitting all markers on the screen
 map.fitBounds(bounds);
 }

my array returns data like e.g _arrSitesLatLang = [[1,1],[1,2]] eg.

There is another Solution for this kind of work

if you use an array main and push in this array the objects and its properties. like

$.grep function 
answered Feb 19, 2015 at 10:30
Sign up to request clarification or add additional context in comments.

Comments

0

You need to do item[0] because item is also an array of length 1.

for(var i = 0; i < data.length; i++) {
 data[i].infoType.forEach(function(item) {
 console.log(item[0]);
 })
}

Working Fiddle

answered Feb 19, 2015 at 10:24

Comments

0

You can use this snippet to flatten your structure:

function getValues(root, vals) {
 if (root instanceof Array){
 for (var i = 0; i < root.length;i++){
 if (typeof root[i] == 'object')
 getValues(root[i], vals);
 else
 vals.push(root[i]);
 }
 return;
 }
 for (var key in root){
 getValues(root[key], vals);
 }
}
var data = [
 {
 infoType: [
 [1],
 [2],
 [3],
 ]
 },
 {
 infoType: [
 [4],
 [5],
 [6],
 ] 
 },
 {
 infoType: [
 [7],
 [7],
 [8],
 ] 
 },
 {
 infoType: [
 [9],
 [10],
 [11],
 ] 
 }
];
var vals = [];
getValues(data, vals);
console.log(vals);

After the structure is flat you just can itarte over the vals variable as a simple array:

for (var i = 0; i< vals.length; i++){
 console.log(vals[i]);
}
answered Feb 19, 2015 at 10:23

Comments

0

To print all values in the above structure, and assuming no other structure is introduced you can do:

Fiddle

for (var i = 0; i < data.length; i++) {
 var obj = data[i].infoType;
 for (var j =0; j < obj.length; j++) {
 console.log(obj[j][0]);
 }
}
answered Feb 19, 2015 at 10:24

Comments

0

this can be also helpful... to print values...

data.map( a => a.infoType.map( b => b[0] ) ).flat()
answered Aug 25, 2022 at 19:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.