Return 如果它是第一个非空数组则为真
Return true if it's first non-empty array
我有一个包含两种类型数据的对象:Array
和 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"
}
现在我正在遍历所有值,我只想 return 第一个非空数组(在本例中是 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?
}
}
},
但是如您所见,我在检查该值不为空后卡住了。接下来我应该写什么?
这是一个棘手的问题,因为 Javascript 对象的属性没有 排序。
也就是说,很难return先这样的属性空值,因为no loop through the object properties is guaranteed to hit them in the same order。作为推论,如果 属性 是对象中的第一个这样的值,那么 return true
是不可能的,因此您的问题无法如所述解决:)
如果你只有 一个 属性 非空长度,你可以做的最好的是:
function FirstNonEmptyArray(object) {
for (var property in object) {
if (object.hasOwnProperty(property) && (object[property] instanceof Array)) {
if (object[property].length > 0) {return property;}
}
}};
如果您有多个具有非空长度的属性,那么,无法保证对象的迭代顺序。这些属性中的任何一个都可以 returned。
最好将 属性 名称附加到数组,如果它们的长度不为零,然后按您的意愿处理它们:
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;
}};
希望这段代码有用。
Js 对象不保证其 key.So 的顺序只查找那些数组和非空的键
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
我有一个包含两种类型数据的对象:Array
和 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"
}
现在我正在遍历所有值,我只想 return 第一个非空数组(在本例中是 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?
}
}
},
但是如您所见,我在检查该值不为空后卡住了。接下来我应该写什么?
这是一个棘手的问题,因为 Javascript 对象的属性没有 排序。
也就是说,很难return先这样的属性空值,因为no loop through the object properties is guaranteed to hit them in the same order。作为推论,如果 属性 是对象中的第一个这样的值,那么 return true
是不可能的,因此您的问题无法如所述解决:)
如果你只有 一个 属性 非空长度,你可以做的最好的是:
function FirstNonEmptyArray(object) {
for (var property in object) {
if (object.hasOwnProperty(property) && (object[property] instanceof Array)) {
if (object[property].length > 0) {return property;}
}
}};
如果您有多个具有非空长度的属性,那么,无法保证对象的迭代顺序。这些属性中的任何一个都可以 returned。
最好将 属性 名称附加到数组,如果它们的长度不为零,然后按您的意愿处理它们:
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;
}};
希望这段代码有用。 Js 对象不保证其 key.So 的顺序只查找那些数组和非空的键
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