Javascript has a built in JSON parse for strings, which I think is what you have:
var myObject = JSON.parse("my json string");
to use this with your example would be:
var jsonData = JSON.parse(myMessage);
for (var i = 0; i < jsonData.counters.length; i++) {
var counter = jsonData.counters[i];
console.log(counter.counter_name);
}
Here is a working example
EDIT: There is a mistake in your use of for loop (I missed this on my first read, credit to @Evert for the spot). using a for-in loop will set the var to be the property name of the current loop, not the actual data. See my updated loop above for correct usage
IMPORTANT: the JSON.parse method wont work in old old browsers - so if you plan to make your website available through some sort of time bending internet connection, this could be a problem! If you really are interested though, here is a support chart (which ticks all my boxes).
Javascript has a built in JSON parse for strings, which I think is what you have:
var myObject = JSON.parse("my json string");
to use this with your example would be:
var jsonData = JSON.parse(myMessage);
for (var i = 0; i < jsonData.counters.length; i++) {
var counter = jsonData.counters[i];
console.log(counter.counter_name);
}
Here is a working example
EDIT: There is a mistake in your use of for loop (I missed this on my first read, credit to @Evert for the spot). using a for-in loop will set the var to be the property name of the current loop, not the actual data. See my updated loop above for correct usage
IMPORTANT: the JSON.parse method wont work in old old browsers - so if you plan to make your website available through some sort of time bending internet connection, this could be a problem! If you really are interested though, here is a support chart (which ticks all my boxes).
In a for-in-loop the running variable holds the property name, not the property value.
for (var counter in jsonData.counters) {
console.log(jsonData.counters[counter].counter_name);
}
But as counters is an Array, you have to use a normal for-loop:
for (var i=0; i<jsonData.counters.length; i++) {
var counter = jsonData.counters[i];
console.log(counter.counter_name);
}
Videos
You're still using double quotes around x.
var x = '[{"k":"1"}]';
Use JSON.parse to turn it into an array:
var array = JSON.parse(x);
console.log(array); // [{ "k": "1" }]
Does it need to an array of objects containing strings?
It seems you are over complicating it, try
//create a javascript object as x, set the VALUE (1) of KEY (k)
var x = {"k":"1"};
//log the objects k property
console.log(x.k);
Just realised what you are trying to achieve, go with the below answer if you're trying to parse JSON that's echo'd via PHP or something similar.
Example parsing JSON to a javascript array
var jsonData = '[{"name" : "Apple"}, {"name" : "Pear"} ]';
var parsedJson = JSON.parse(jsonData);
console.log(parsedJson[0]);
parsedJson.forEach(function(fruit){
console.log(fruit.name);
});
Thought these two operation can be done together for clean writing you can do the following.
map to add the difference between the two property in a new property called d
using Math.abs to keep the difference positive.
let teams = [{"team":"Arsenal","f":"79","a":"36"},
{"team":"Liverpool","f":"67","a":"30"},
{"team":"Manchester_U","f":"87","a":"45"},
{"team":"Newcastle","f":"74","a":"52"},
{"team":"Leeds","f":"53","a":"37"},
{"team":"Chelsea","f":"66","a":"38"},
{"team":"West_Ham","f":"48","a":"57"},
{"team":"Aston_Villa","f":"46","a":"47"},
{"team":"Tottenham","f":"49","a":"53"},
{"team":"Blackburn","f":"55","a":"51"},
{"team":"Southampton","f":"46","a":"54"},
{"team":"Middlesbrough","f":"35","a":"47"},
{"team":"Fulham","f":"36","a":"44"},
{"team":"Charlton","f":"38","a":"49"},
{"team":"Everton","f":"45","a":"57"},
{"team":"Bolton","f":"44","a":"62"},
{"team":"Sunderland","f":"29","a":"51"},
{"team":"Ipswich","f":"41","a":"64"},
{"team":"Derby","f":"33","a":"63"},
{"team":"Leicester","f":"30","a":"64"}];
teams.map((t) => t['d'] = Math.abs(t.f - t.a))
console.log(teams);
To reduce to the team that has the lowest difference you can use the below code with reduce
let teams = [{"team":"Arsenal","f":"79","a":"36"},
{"team":"Liverpool","f":"67","a":"30"},
{"team":"Manchester_U","f":"87","a":"45"},
{"team":"Newcastle","f":"74","a":"52"},
{"team":"Leeds","f":"53","a":"37"},
{"team":"Chelsea","f":"66","a":"38"},
{"team":"West_Ham","f":"48","a":"57"},
{"team":"Aston_Villa","f":"46","a":"47"},
{"team":"Tottenham","f":"49","a":"53"},
{"team":"Blackburn","f":"55","a":"51"},
{"team":"Southampton","f":"46","a":"54"},
{"team":"Middlesbrough","f":"35","a":"47"},
{"team":"Fulham","f":"36","a":"44"},
{"team":"Charlton","f":"38","a":"49"},
{"team":"Everton","f":"45","a":"57"},
{"team":"Bolton","f":"44","a":"62"},
{"team":"Sunderland","f":"29","a":"51"},
{"team":"Ipswich","f":"41","a":"64"},
{"team":"Derby","f":"33","a":"63"},
{"team":"Leicester","f":"30","a":"64"}];
teams.map((t) => t['d'] = Math.abs(t.f - t.a))
// now finding the team with the lowsest
let result = teams.reduce((teamA,teamB) => teamA.d > teamB.d? teamB: teamA );
console.log(result);
Both using loop (e.g. for...of ) or array methods (e.g. map for the part 1 and filter for part 2) are valid approach.
I personally prefer loop because it is easier to read and debug and you can combine both steps into one loop. something like:
// use the first element as starter then update in the loop
let leastDiff = Math.abs(myArray[0].f - myArray[0].a);
let bestTeam = myArray[0].team;
for (let item of myArray) {
item.diff = Math.abs(item.f - item.a);
if (item.diff < leastDiff) {
leastDiff = item.diff;
bestTeam = item.team;
}
}
console.log(`Team with least difference is ${bestTeam} with ${leastDiff} goal difference`);
An easier method is to map to the Number object
result= stringBits.map(Number);
javascript 1.6. has map() ( https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/Map ), so you can do something like
intArray = someArray.map(function(e) { return parseInt(e) })