CodingCan't get value from key when obj is imported

 

Press Ctrl+Enter to quickly submit your post
Quick Reply  
 
 
  
 From:  CHYRON (DSMITHHFX)  
 To:  ALL
41790.1 
I'm messing around object key value stuffs and hit a wall:

If I define an object's key:value pairs in the script and then get the value of a certain key, it works fine.

If I $.get  an external txt file then it returns "undefined" for the value. If I iterate through the same imported file using $.each, it'll faithfully list each key:value pair.
Code: 
$(document).ready(function() {    
    
// DATE    
    var now = new Date(Date.now());
    var mond = function() {
        return ('0' + (now.getMonth() + 1)).slice(-2);
    }
    var dayd = function() {
        return ('0' + now.getDate()).slice(-2);
    }
    var dayKey = mond() + dayd();
    $.get('alldatestest.txt', function(data) {
        var picsObj = {data};
        alert(picsObj.dayKey);
        /*$.each(picsObj, function(key,value) {
            //alert(key + ':' + value);
            if ( key = dayKey ) {
                //alert('yes');
                alert(value);
            }
        });*/
    });
});    

Here's the external file as reference above:
Code: 
0810: '',
0811: '100315/5065359-R1-19-20_scaled.jpg',
1004: '100315/5065359-R1-19-20_scaled.jpg',
1008: 'images/CNV00016-interim.jpg'
A simple trick makes it terrifyingly easy to see whether or not someone has muted you on Twitter
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  CHYRON (DSMITHHFX)     
41790.2 In reply to 41790.1 
var picsObj = JSON.parse('{'+data+'}');
0/0
 Reply   Quote More 

 From:  CHYRON (DSMITHHFX)  
 To:  Peter (BOUGHTONP)     
41790.3 In reply to 41790.2 
Thanks, I'll try it in the am.
A simple trick makes it terrifyingly easy to see whether or not someone has muted you on Twitter
0/0
 Reply   Quote More 

 From:  koswix  
 To:  CHYRON (DSMITHHFX)     
41790.4 In reply to 41790.3 
Did it work?

 ▪                    
             ┌────┐    ┌────┐                      
          │    │    │    │ ▪                    
          │    └────┘    │                      
          │   ──┐  ┌──   │ ▪                    
   ┌──────┤    ▪    ▪    │                      
  ┌┘      │              │ ▪                    
┌─┤       └──┐  │  │  ┌──┘                      
│ │          │ ││  ││ │   ┌─┐                   
│ │          └─┼┤  └┴─┴───┘ │                   
│ │           ─┘│           │                   
│ │   ┌──────┐  └┬──────────┘                   
  │   │      │   │                              
  │   │      │   │                              
  └───┘      └───┘                              
If Feds call you and say something bad on me, it may prove what I said are truth, they are afraid of it.
0/0
 Reply   Quote More 

 From:  CHYRON (DSMITHHFX)  
 To:  ALL
41790.5 
Seems to be working now. I had to stick the brackets in the external file & also change its single quotes to double, and switch .dayKey to [dayKey] in the script:
Code: 

    $.get('alldatestest.txt', function(data) {
        var picsObj = JSON.parse(data);
        alert(dayKey + ' ' + picsObj[dayKey]);
    });
- returns "0812 100315/5065359-R1-19-20_scaled.jpg"

This was also helpful: http://json.parser.online.fr/

Thanks Peter!

Edit: to clarify, here's the new "alldatestest.txt" -- I also had to wrap keys in quotes per above online parser:
 
Code: 
{"0810":"images/CNV00016-interim.jpg",
"0812": "100315/5065359-R1-19-20_scaled.jpg",
"1004": "100315/5065359-R1-19-20_scaled.jpg",
"1008": "images/CNV00016-interim.jpg"}
A simple trick makes it terrifyingly easy to see whether or not someone has muted you on Twitter
0/0
 Reply   Quote More 

 From:  CHYRON (DSMITHHFX)  
 To:  ALL
41790.6 
And now you can browse back and forth through the Daily Bad Pictures*, thanks to stack overflow and their diabolical spell
 
Code: 
Object.keys(obj).indexOf(key)


(you're not supposed to be able to do indexy stuffs on objects, but there you have it  (angel) )

http://www.designartcraft.com/photo/afbp.htm

*subject to buggery (NJ), breakage and general fail

A simple trick makes it terrifyingly easy to see whether or not someone has muted you on Twitter
0/0
 Reply   Quote More 

Reply to All    
 

1–6

Rate my interest:

Adjust text size : Smaller 10 Larger

Beehive Forum 1.5.2 |  FAQ |  Docs |  Support |  Donate! ©2002 - 2020 Project Beehive Forum

Forum Stats