I Have following JSON data, I need to iterate this data based on Keytype in JAVASCRIPT.
It should return record from only the passed key code.
Say i need record from 438 means it should give me only following data.
(ex:"K": "43800001", "D": "Data1")
{
"GroupCode": {
"PickType": [
{
"#KeyType": "438",
"R": [
{
"K": "43800001",
"D": "Data1"
}
]
},
{
"#KeyType": "439",
"R": [
{
"K": "43900001",
"D": "Data2"
}
]
},
{
"#KeyType": "440",
"R": [
{
"K": "44000001",
"D": "Data3"
}
]
},
{
"#KeyType": "441",
"R": [
{
"K": "44100001",
"D": "Data4"
}
]
}
]
}
}
as I'm not good in java script and new to it, i haven't tried coding for this. Please help me in getting the data.
Let us say the above is stored in a variable obj. You can get the result via following
var result = obj.GroupCode.PickType.filter(function(item){
return item["#KeyType"] === "438"
}).map(function(item){
return item.R[0];
});
Please note, result is an array. If you have unique object against the condition then for that you will have to check the length of array and then extract the object i.e. result[0] or result.shift()
Working Fiddle here
function getByKeyType(keytype) {
for(i in f.GroupCode.PickType) {
if(f.GroupCode.PickType[i].KeyType == keytype) {
return f.GroupCode.PickType[i].R[0];
}
}
return null;
}
alert(getByKeyType(438).K)
alert(getByKeyType(438).D)
Try this
function getByKeyType(keytype) {
for(i in f.GroupCode.PickType) {
if(f.GroupCode.PickType[i].KeyType == keytype) {
return f.GroupCode.PickType[i].R[0];
}
}
return null;
}
Try a simple jquery each loop and a if:
jquery:
$.each(obj.GroupCode.PickType,function(i,v){
if (v["#KeyType"] == 438) {
console.log(v.R)//v.R[0];
}
});
javascript:
for(v in obj.GroupCode.PickType) {
if (v["#KeyType"] == 438) {
console.log(v.R)//v.R[0];
}
}
Related
consider this example
let obj = {
"minute": {
"data": [
{
"s": [0],"values": {
"TV Web": {
"P": [0],
"S": [0]
}
}
},
{ },
],
"product": ["Tom"]
},
"ads": { },
}
for(let entry in obj){
console.log(obj[entry].product[0])
}
if you run the snippet it shows "TOM" and undefined.
How can I select only Tom? without having undefined?
you can just directly access to that path obj["minute"].product[0];
if you want check in all of property, then you can skip undefined value
for(let entry in obj){
if (obj[entry].product){
console.log(obj[entry].product[0]);
}
// or use ternari
obj[entry].product && console.log(obj[entry].product[0]);
// or use library like `lodash`
console.log(lodash.get(obj[entry], "product[0]"));
}
How do I sort this array of objects by checking the objects' key?
const list = [
{ "firstValue": { "a": "1" } },
{ "secondValue": { "b": "1" } },
{ "thirdValue": { "c": "1" } },
{ "fourthValue": { "d": "1" } },
{ "fifthValue": { "e": "1" } }
]
So in this case I want it to be sorted like:
const list = [
{ "fifthValue": { "e": "1" } },
{ "firstValue": { "a": "1" } },
{ "fourthValue": { "d": "1" } },
{ "secondValue": { "b": "1" } },
{ "thirdValue": { "c": "1" } }
]
This is my current code:
sortArrOfObjectsByKey(list);
document.write(JSON.stringify(list))
function sortArrOfObjectsByKey(arrToSort) {
arrToSort.sort(function(a, b) {
return a < b ? -1 : 1
});
}
But it doesn't get sorted correctly.
Please note that there shouldn't be any new container for the new sorted array/object.
Help! Thanks
You need to fetch property name and then sort according to that
const list = [
{ "firstValue": { "a": "1" } },
{ "secondValue": { "b": "1" } },
{ "thirdValue": { "c": "1" } },
{ "fourthValue": { "d": "1" } },
{ "fifthValue": { "e": "1" } }
]
list.sort( function(a, b) {
const aKey = Object.keys(a)[0];
const bKey = Object.keys(b)[0];
return aKey.localeCompare(bKey)
} );
console.log(list)
I have a php script returning JSON encoded data like below:
{
"1":
{
"from":"Vimbai Jongwe",
"msg":"wadii"
},
"2":
{
"from":"Brian Dube",
"msg":"Eh Chibaba"
}
}
So now i want to access that data using the keys from and msg to be displayed somewhere.
You can loop it like below
var data = {
"1":
{
"from":"Vimbai Jongwe",
"msg":"wadii"
},
"2":
{
"from":"Brian Dube",
"msg":"Eh Chibaba"
}
}
for(var d in data){
console.log(data[d].from +"->"+data[d].msg);
}
Try below code you can loop and print as in below code
a = {
"1":
{
"from":"Vimbai Jongwe",
"msg":"wadii"
},
"2":
{
"from":"Brian Dube",
"msg":"Eh Chibaba"
}
};
for(var i in a){
console.log(a[i].from);
console.log(a[i].msg);
}
I have a series of nested objects like this:
data = {"12345":{"value":{"1":"2","3":"4"}},
{"12346":{"value":{"5":"6","7":"8"}},
{"12347":{"value":{"9":"0","11":"22"}}
I would like to create a function to grab certain objects within this grouping. For example...
grabObject(12345);
would return:
{"value":{"1":"2","3":"4"}}
Any help you could provide would be great.
You don't need anything more than this:
function grabObject(id) {
return data[id];
}
After making some fixes to your syntax, here's a working jsFiddle: http://jsfiddle.net/jfriend00/04no0bvm/
var data = [
{
"12345": {
"value": {
"1": "2",
"3": "4"
}
}
},
{
"12346": {
"value": {
"5": "6",
"7": "8"
}
}
},
{
"12347": {
"value": {
"9": "0",
"11": "22"
}
}
}
];
function grabObject(id) {
var result;
for (i = 0; i < data.length; i++) {
for (var k in data[i]){
if(k == id) {
result = data[i][k];
}
}
}
return result;
}
console.log(grabObject('12345'));
This is the code I tested check and let me know
I have a JSON string which looks like this:
[
{
"queryResult": {
"A": "12-04-2014",
"B": 1
}
},
{
"queryResult": {
"A": "13-04-2014",
"B": 2
}
},
{
"queryResult": {
"A": "14-04-2014",
"B": 3
}
}
]
And I need to parse it and change it to this
[
{
"A": "12-04-2014",
"B": 1
},
{
"A": "13-04-2014",
"B": 2
},
{
"A": "14-04-2014",
"B": 3
}
]
I already have a function for making that change, which is:
function justAnExample() {
var jsonData = exampleJSON(); //Obtains the JSON
var finalJSON=JSON.stringify(jsonData[0]['queryResult']);
for (var i = 1; i < jsonData.length; i++) {
finalJSON = finalJSON+','+JSON.stringify(jsonData[i]['queryResult']);
}
return JSON.parse('[' + finalJSON + ']');
}
But, this method uses stringifying and then parsing JSON to recreate the JSON object, which works, but is there a better solution, in which I can work with the object notation itself.
P.S: I know the term "JSON object" is a semi-pseudo thing, and that only the JSON notation/format matters, but, just need to confirm if this is the proper way to do it.
Edit
Please find a JS fiddle for the problem
http://jsfiddle.net/mukilr/uJV54/
You can do:
var json = [
{
"queryResult": {
"A": "12-04-2014",
"B": 1
}
},
{
"queryResult": {
"A": "13-04-2014",
"B": 2
}
},
{
"queryResult": {
"A": "14-04-2014",
"B": 3
}
}
];
var out = [];
for (var i = 0; i < json.length; i++){
out[i] = json[i].queryResult;
}
check this fiddle
EDIT
This is your fiddle updated