You could use Array.map here
var arr = oldArr.map(function(obj){
var key = Object.keys(obj).sort()[1], rtn = {};
return rtn[key] = obj[key], rtn;
});
What you're doing is taking the second key and then returning a new Object with that key and value.
Answer from Amit Joki on Stack OverflowYou could use Array.map here
var arr = oldArr.map(function(obj){
var key = Object.keys(obj).sort()[1], rtn = {};
return rtn[key] = obj[key], rtn;
});
What you're doing is taking the second key and then returning a new Object with that key and value.
var initialArr = [
{"a1" : "a1Val", "a2" : "a2Val", "a3" : "a3Val"},
{"b1" : "b1Val", "b2" : "b2Val", "b3" : "b3Val"},
{"z1" : "z1Val", "z2" : "z2Val", "z3" : "z3Val"}
];
var resArr = initialArr.map(function(x){
return x[Object.keys(x)[1]];
});
jsFiddle Demo
Here in this resArr, I am mapping it to initialArr[ ], to generate an array out of it called resArr and the each element initialArr is represented as 'x' in this x is an object now inorde to get the 2nd element of the object x[1] so here, 1 represents the second element as index starts from 0,1 ,,,so we need to get the keys of that object by Object.keys(x)[1]...hence value would be x[Object.keys(x)[1]].
Here is a JsFiddle Example
var json = '{}' // your data;
// convert to javascript object:
var obj = JSON.parse(json);
// get last item in array:
var last = obj.education[obj.education.length - 1].school.name;
// result: some COLLEGE NAME I WANT TO GET
If your json above was saved to an object called json, you could access the school name "some COLLEGE NAME I WANT TO GET" with the following:
json.education[2].school.name
Use map. It's much simpler:
var arr = data.personnes.map(function (el) {
return el.departement;
});
console.log(arr); // ["bas-rhin", "eure"]
Alternatively, using jQuery's $.map:
var arr = $.map(data.personnes, function (el) {
return el.departement;
});
Fiddle
If you need a polyfill for map:
if (!('map' in Array.prototype)) {
Array.prototype.map = function (mapper, that /*opt*/) {
var other = new Array(this.length);
for (var i = 0, n = this.length; i < n; i++) {
if (i in this) { other[i] = mapper.call(that, this[i], i, this); }
}
return other;
};
}
I think you should try like this:
$.getJSON( "ajax/test.json", function( data ) {
console.log('loaded');
var departement = []; // create array here
$.each(data.personnes, function (index, personne) {
departement.push(personne.departement); //push values here
});
console.log(departement); // see the output here
});
var json_data = {"2013-01-21":1,"2013-01-22":7};
var result = [];
for(var i in json_data)
result.push([i, json_data [i]]);
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Slices');
data.addRows(result);
http://jsfiddle.net/MV5rj/
If you have a well-formed JSON string, you should be able to do
var as = JSON.parse(jstring);
I do this all the time when transfering arrays through AJAX.
You need to loop through the array and then parse the stringified JSON so that you can access the data array. Then simply loop that data array to get the value of each name property.
var arr = [{
"assetName": "LCT",
"assetValue": "",
"typeValueInput": "select",
"valueInputSelect": null,
"required": true,
"valueInput": "{\"data\":[{\"name\":\"name1\",\"id\":\"12\"},{\"name\":\"name2\",\"id\":\"13\"},{\"name\":\"name3\",\"id\":\"14\"}]}"
}];
arr.forEach((arrObj) => {
var jsonData = JSON.parse(arrObj.valueInput);
jsonData.data.forEach(({name}) => console.log(name));
});
You could use JSON.parse
var jsonArray = [
{
assetName: 'LCT',
assetValue: '',
typeValueInput: 'select',
valueInputSelect: null,
required: true,
valueInput:
'{"data":[{"name":"name1","id":"12"},{"name":"name2","id":"13"},{"name":"name3","id":"14"}]}'
}
];
let name = jsonArray[0].valueInput;
name = JSON.parse(name);
name.data.forEach(value => {
console.log(value.name, value.id);
});
You can loop the array with a for loop and the object properties with for-in loops.
for (var i=0; i<result.length; i++)
for (var name in result[i]) {
console.log("Item name: "+name);
console.log("Source: "+result[i][name].sourceUuid);
console.log("Target: "+result[i][name].targetUuid);
}
Use a loop
for(var i = 0; i < obj.length; ++i){
//do something with obj[i]
for(var ind in obj[i]) {
console.log(ind);
for(var vals in obj[i][ind]){
console.log(vals, obj[i][ind][vals]);
}
}
}
Demo: http://jsfiddle.net/maniator/pngmL/
That's because your base object is an array as well.
console.log(dataJS[0].comments[0]);
I suspect that would work
the JSON you have coming back is actually an array itself, so...
dataJS[0].comments[0].created_at
will be 2011-02-09T14:42:42-08:00, etc...
Both dataJS and comments are arrays, and need indexes to access the appropriate elements.
Here is the idea :
JSONObject root = new JSONObject(yourJsonString);
JSONArray sportsArray = root.getJSONArray("sports");
// now get the first element:
JSONObject firstSport = sportsArray.getJSONObject(0);
// and details of the first element
String name = firstSport.getString("name"); // basketball
int id = firstSport.getInt("id"); // 40
JSONArray leaguesArray = firstSport.getJSONArray("leagues");
// and so on, you can process leaguesArray similarly
It should work (feel free to complain about compile errors if there are any)
Your JSON data is an object (it starts with a curly brace). In the next inner layer, there is a single array (at key "sports"):
String jsonString = readURL("//my URL is here");
JSONObject result = JSONObject(jsonString);
JSONArray sports = result.getJSONArray("sports");
JSONObject sport = sport.getJSONObject(0);
System.out.println(sport.getString("name"));
I might have used another JSON library than you.