I am trying to create an array in the following format:
// Required JSON format to create javascript dynamically
[{id:0,[{'val':1,'bool':true},{'val':2,'bool':true}]}]
I tried using the javascript push method as follows:
var arr2 = [];
var arr3 = arr2.push({'id':0,'value':1,'bool':true});
But this is not giving the desired format. I don't know how to group by a common id. Anyone knows please help me.
Because it isn't a valid object, your array of { val:..,'bool': .. } needs a key. Like so:
var ob = { id: 0,
meaningfulName: [
{'val':1,'bool':true},
{'val':2,'bool':true}
]};
Also having object keys of 'val' and 'bool' aren't very meaningful, choose a name that represents what they are.
Related
I send a php array like:
$var = array (
0=> 4,
1=> 6,
2=> 8,
...
as json_encode($var); into the uri and then I receive it into javascript file is still ok here but when I push it into new array like this :
this.patg.push(attd);
Is inserted like this below .
var attds = ["4,6,7,8,9,5558,5560,5573,5574,5586,5589,5606"]
I know I have to find the problem. but
Questions:
could you please tell me why this could happends or help me to deal with it.
but in any case just for knowledge . how you would add the extra " " surrounding the , that i miss to be an array , or is that crazy idea to fix this?
If you are receiving a string value and you want to use it as an array of integers you should split it into an array first:
var receivedData = "4,6,7,8,9,5558,5560,5573,5574,5586,5589,5606";
var dataArray = receivedData.split(",");
Afterwards you can use it with another array, however be aware that if you already have a defined array into which you want to push the dataArray you shouldn't push but concat instead.
In other words if you have:
var previousArray = [1,2,3];
previousArray.push(dataArray);
You will get
[1,2,3,[4,6,7,8,9,5558,5560,5573,5574,5586,5589,5606]]
meaning that the whole array is pushed onto the 4th position of previousArray.
If, on the other hand, you concat the arrays will merge:
var previousArray = [1,2,3];
previousArray.concat(dataArray);
[1,2,3,4,6,7,8,9,5558,5560,5573,5574,5586,5589,5606]
Source: http://www.w3schools.com/jsref/jsref_concat_array.asp
Couldn't think of better title.
Important to note: I'm new to js and I guess its the reason I can't figure it out by myself.
I have a JSON array returned from a database, the JSON array represent a set of points.
Example of the JSON array:
var JSON_array = [{lat:1,lng:2}, {lat:1,lng:3},{lat:2,lng:3}...]
I want to make a different array whose elements will be a function and the variables of the function will be the elements from the JSON array, like so:
var coordinates = [
new google.maps.LatLng(1, 2),
new google.maps.LatLng(1, 3),
new google.maps.LatLng(2, 3),
....
];
I made a function using forEach that counts the elements of the JSON array (for learning and trying to find a way to what I want) but I cant think of a way make the array mentioned above.
You could use Array map method:
var coordinates = JSON_array.map(function(coordinate) {
return new google.maps.LatLng(coordinate.lat, coordinate.lng);
})
This method gives you an new array based on (1) the original array and (2) how you deal with each element. Here's more detailed doc for the method.
you can also use regular for loop
coordinates = [];
for(var i=0;i<JSON_array.length;i++){
coordinates.push(new google.maps.LatLng(JSON_array[i].lat,JSON_array[i].lng));
}
For mapping element from one array to another you can use Array.prototype.map function like
var JSON_array = [{lat:1,lng:2}, {lat:1,lng:3},{lat:2,lng:3}...];
var coordinates = JSON_array.map(function(el){ return new google.maps.LatLng(el.lat, el.lng)});
As Grundy commented you can just do:
coordinates = JSON_array.map(function(x) {
return new google.maps.LatLng(x.lat, x.lng);
});
You might want to get your nomenclature straight though, this has nothing to do with JSON.
I have my json data (array of map) in my javascript and it's like as below:
[
{"HOURLY":"$309.75","Airport Fee":"$15.00","STC":"$52.66","Gratuity":"$61.95","Fuel Surcharge":"$5.00"},
{"HOURLY":"$309.75","Airport Fee":"$15.00","STC":"$52.66","Gratuity":"$61.95","Fuel Surcharge":"$5.00"}
]
I want to use the values of one of the maps in my jsp using some object.value or map.getValueBykey mechanism as if just like we use a model object passed from java to jsp.
Seeing that you have an array of objects, treat it first like an array. So,
prices = [
{"HOURLY":"$309.75","Airport Fee":"$15.00","STC":"$52.66","Gratuity":"$61.95","Fuel Surcharge":"$5.00"},
{"HOURLY":"$329.75","Airport Fee":"$15.00","STC":"$52.66","Gratuity":"$61.95","Fuel Surcharge":"$5.00"}
];
console.log(prices[0].HOURLY); // will output "$309.75", value found in the first object
console.log(prices[1].HOURLY); // will output "$329.75", value found in the second object
// If you want to get all values of that one property in all objects in the array, you can iterate like this:
for(var i =0;i <prices.length;i++){
console.log( prices[i].HOURLY);
}
// If you want to get all values for each property in a specific object, for example the first one in the array, you can iterate like this :
for(key in prices[0]){
console.log("'" +key + "' = " + prices[0][key]);
}
This is how it's done in JavaScript.
I see you are asking about JSP however. Maybe an example like this is what you are looking for:
javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>
I have a form and would like to append the contents of it to an existing array.
I am using JSON.stringify( $('#myForm').serializeObject() ) to convert my form elements into json objects.
The form has user info that i would like to append into myArr then append this list into an existing array.
myArr is populating fine, its just appending that into existingJsonArray i seem to be having problems with.
I saw this but since JSON.stringify creates the full json array would i need to knock out the [{ and }] ?
Is this the correct approach?
var existingJsonArray = [];
var myArr = [];
myArr.unshift( JSON.stringify( $('#myForm').serializeObject() ) );
existingJsonArray.unshift(myArr);
Please notice that JSON is the string representation of objects - and not suited well for manipulating them.
var array = [], // an Array literal in JavaScript code
formObject;
formObject = $('#myForm').serializeObject(); // an object representing the form
array.unshift([formObject]); // not sure why you need the nested array
// create string containing JSON representation of the array:
var jsonString = JSON.stringify(array);
I'm have trouble finding a way to push values into an array dynamically. I have given the following situation:
var get_anchors= new Array('pitzel','mitzel','sizzle')
current_anchor= pics[key].anchor; //has the value 'sizzle'
get_anchors[current_anchor].push(new Array('sizzle2','sizzle3'))
Javascript fails and says get_anchors[current_anchor] is undefined
How can I make get_anchors[current_anchor] work. Or is there a different way to get this done?
The desired result should look like 'pitzel','mitzel','sizzle'['sizzle2','sizzle3]
Based on your comment it looks like you want a hash map instead of an array. You can use an object for this:
var anchors = {
'pitzel': [],
'mitzel': [],
'sizzle': []
};
Then you can do:
anchors[current_anchor].push('sizzle2', 'sizzle3');
or assuming that anchors does not have a property with the value of current_anchor, simply assign a new array:
anchors[current_anchor] = ['fooX', 'fooY'];
Of course you can populate the object dynamically as well. Have a look at Working with Objects for more information.
I'm not sure I understand what you're trying to do, but I think you're trying to insert a few elements after where another occurs. This will do the trick:
var get_anchors = [ 'pitzel', 'mitzel', 'sizzle' ];
current_anchor = get_anchors.indexOf(pics[key].anchor);
get_anchors.splice(current_anchor + 1, 0, 'sizzle2', 'sizzle3');
// get_anchors = [ 'pitzel', 'mitzel', 'sizzle', 'sizzle2', 'sizzle3' ]