1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to pick only one object from the lish that has data and ignore the rest

Discussion in 'JavaScript' started by ketting00, Feb 4, 2020.

  1. #1
    Hi guys,
    It's me again. I'm checking if objects containg value with Object.keys like below:
    Object.keys(obj).length
    Code (markup):
    It would return the list like this:
    0
    0
    0
    3
    0
    0
    Code (markup):
    The list is unordered and number of items is variable depending on an amount of objects being checked. It would generated automatically. So how do I pick the only one that has data and ignore the rest.
    I do the switch and it isn't working.
    
    var flag;
    switch (Object.keys(obj).length) {
        case 3:
            flag = true;
            break; // => it does not break
        case 0:
            flag = false;
            break;
        default:
            flag = false;
    }
    console.log(flag); // still => false, false, false, true, false, false
    
    Code (markup):
    Doing this doeesn't work either:
    
    var flag;
    if(obj !== 'undefined') {
        flag = true;
    } else {
        flag = false;
    }
    console.log(flag); // false, false, true, false, false, false
    
    Code (markup):
    Thank you,
     
    Last edited: Feb 4, 2020
    ketting00, Feb 4, 2020 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    What on earth are you feeding that for values that returns multiple keys that equals zero?

    I'm not following what it is you're trying to do here whatsoever. Doesn't make a lick of sense.... what's the "list" / data being processed?!? What about it do you actually want to know? What are you doing where the number of keys is something you want to know?
     
    deathshadow, Feb 5, 2020 IP
  3. ketting00

    ketting00 Well-Known Member

    Messages:
    772
    Likes Received:
    27
    Best Answers:
    3
    Trophy Points:
    128
    #3
    Thank for response.

    As far as I researched, it has to write a counter to count an object with data only. So how to count it?
    Here's what the law data look like:
    
    {}
    {}
    {}
    { name: 'John', surname: 'Doe' }
    {}
    {}
    
    Code (markup):
     
    ketting00, Feb 5, 2020 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    What I'm not getting is why you even have empty objects, and how those objects are stored. Are they in an array?

    The solution to my mind is to fix the data source so it's not vomiting up empty records, or purging the data of empty records since NONE of them should be serving a legitimate purpose.

    Also, what do you mean by "it"? As in "It has to write"... what has to write? FROM what?
     
    deathshadow, Feb 5, 2020 IP
  5. ketting00

    ketting00 Well-Known Member

    Messages:
    772
    Likes Received:
    27
    Best Answers:
    3
    Trophy Points:
    128
    #5
    Thank you,

    I think the method isn't good though. I would stop using it and find alternative.
     
    ketting00, Feb 5, 2020 IP