I hope.. It should do the trick
var inputString=[{"rKVAH_Lag":"xyz","Asset_Name":"GSM Meter"}];
var keys=[]
var values=[]
i=0
for (var key in inputString[0]) {
keys[i]=key;
values[i]=inputString[0][key]
i=i+1
}
console.log(keys);
console.log(values);
Answer from Mahesh on Stack Overflowhow to get value of json array object in javascript or jquery - Stack Overflow
javascript - extract array from json object - Stack Overflow
javascript - get values in pairs from json array - Stack Overflow
How to get values from json array in a javascript - Stack Overflow
I hope.. It should do the trick
var inputString=[{"rKVAH_Lag":"xyz","Asset_Name":"GSM Meter"}];
var keys=[]
var values=[]
i=0
for (var key in inputString[0]) {
keys[i]=key;
values[i]=inputString[0][key]
i=i+1
}
console.log(keys);
console.log(values);
Maybe this works for you.
var obj = { a: 1, b: 2, c: 3, d: 4, e: 5 },
keys = Object.keys(obj),
values = keys.map(function (k) { return obj[k]; });
document.write('<pre>' + JSON.stringify(keys, 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(values, 0, 4) + '</pre>');
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.
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]].
So, you have an array of key/value pairs. Loop the array, at each index, log each pair:
var obj = [{"oid":"2","cid":"107"},{"oid":"4","cid":"98"},{"oid":"4","cid":"99"}];
for (var i = 0; i < obj.length; i++) {
console.log("PAIR " + i + ": " + obj[i].oid);
console.log("PAIR " + i + ": " + obj[i].cid);
}
Demo: http://jsfiddle.net/sTSX2/
This is an array that you have //lets call it a:
[{"oid":"2","cid":"107"},{"oid":"4","cid":"98"},{"oid":"4","cid":"99"}]
To get first element :
a[0] // this will give you first object i.e {"oid":"2","cid":"107"}
a[0]["oid"] // this will give you the value of the first object with the key "oid" i.e 2
and so on ...
Hope that helps. `
Hi!
I have a fragment of code that executes an ajax request and returns me this json (formatted a little to look decent on reddit):
[{"id":698,"song_name":"never too late","song_artist":"Three Days Grace","song_album":"One-X","song_duration":"00:03:29"},
{"id":501,"song_name":"new romantics","song_artist":"Taylor Swift","song_album":"1989","song_duration":"00:03:57"},
{"id":686,"song_name":"no reason","song_artist":"Sum 41","song_album":"Chuck","song_duration":"00:03:04"},
{"id":909,"song_name":"now my feet won't touch the ground","song_artist":"Coldplay","song_album":"Prospekt's March EP","song_duration":"00:02:30"},
{"id":517,"song_name":"numb","song_artist":"Linkin Park","song_album":"Meteora","song_duration":"00:03:49"}]I've been trying to access the fields of this json object for a while now but I cannot figure out for the life of me what i'm doing wrong. I am very new to javascript so it's probably something very silly.
my code:
function querySongs() {
let song = 'n'
let xhr = new XMLHttpRequest()
xhr.open('GET', 'http://127.0.0.1:8000/music_tracker/get_song_by_name/?song_name=' + song, true)
xhr.setRequestHeader("Content-Type", 'application/json;')
xhr.onload = function() {
if (xhr.status == 200) {
let songs = JSON.parse(this.response)
console.log(songs) // this prints array indexes
for(var song in songs){
console.log(song.song_name) // this one gives undefined
}
}
}
xhr.send()
}Thank you in advance for your help! :)