【发布时间】:2016-08-01 01:11:34
【问题描述】:
我有一个包含两种类型数据的对象: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"
}
现在我正在遍历所有值,并且我只想返回第一个非空数组(在本例中为 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 arrays vue.js