1

I have an object with two types of data: Array and String:

{
  "livingroom": [
    {
      "app": "",
      "name": "s1",
      "thumbnail": "https://storage.googleapis.com/peterbucket/istagingViewer/sigstaging.com.tw/Cuiti/study/web/thumb_floorplan1.jpg"
    }
  ],
  "study": [
    {
      "app": "",
      "name": "s0",
      "thumbnail": "https://storage.googleapis.com/peterbucket/istagingViewer/sigstaging.com.tw/Cuiti/study/web/thumb_floorplan3.jpg"
    }
  ],
  "outdoor": [],
  "id": "-KF28-_Vdve-u3498eQ1",
  "name": "Cuiti"
}

Right now I'm looping through all the values, and I want to return only the first non-empty array (in this case the value of livingroom).

// Template
<div v-for="value in object" v-if="isFirstNonEmptyArray(value, object)">

// JavaScript
isFirstNonEmptyArray (value, object) {
  if (value instanceof Array) {
    if (value.length > 0) {
      // What to do here?
    }
  }
},

But as you can see I'm stuck after checking that the value is not empty. What should I write next?

alex
  • 7,111
  • 15
  • 50
  • 77
  • You shouldn't rely on the order of an object's properties. If you want to keep order, use an array. – James Apr 11 '16 at 03:12

2 Answers2

4

This is a tricky question because Javascript objects do not have an ordering on their properties.

In other words, it is very difficult to return the first such property with empty value because no loop through the object properties is guaranteed to hit them in the same order. As a corollary, it is therefore impossible to return true if that property is the first such value in the object, so your problem cannot be solved as stated :)

If you have only one property with a non-empty length, the best you can do is the following:

function FirstNonEmptyArray(object) {
    for (var property in object) {
        if (object.hasOwnProperty(property) && (object[property] instanceof Array)) {
            if (object[property].length > 0) {return property;}
       }
}};

If you have multiple properties that have non-empty length, well, iteration order through an object is not guaranteed. Any one of those properties could be returned.

It's much better to append property names to an array if they have non-zero length and then do with them as you will:

 function AllNonEmptyArrays(object) {
    var array = []
    for (var property in object) {
        if (object.hasOwnProperty(property) && (object[property] instanceof Array)) {
            if (object[property].length > 0) {array.push(property);}
       }
    return array;
}};
Community
  • 1
  • 1
Akshat Mahajan
  • 9,543
  • 4
  • 35
  • 44
1

Hope this code will be useful. Js obejct does not guarantee the ordering of its key.So finding out only those keys which are array and non empty

var _localArray = [] // Use to store non empty array 
    for(var keys in a){ // Iterating over object. a is the object
    // Checking if the current key is an array & it's length is >= 1
    if(Object.prototype.toString.call(a[keys] ) === '[object Array]' && a[keys].length>=1) {
      _localArray.push(a[keys]);  // push the key in temp array
    }
    }
    console.log(_localArray[0]); // will log the first non empty array

jsfiddle

brk
  • 48,835
  • 10
  • 56
  • 78