2015-05-22 38 views
0
{ 
    "APPLICATION_DETAIL":[ 
    { 
     "Application1":"", 
     "Application2":"", 
     "status":{ 
      "Application1":"Active", 
      "Application2":"Inactive" 
     }, 
     "modifiedBy":"a123453" 
     }, 
     { 
     "Application1":"", 
     "Application2":"W", 
     "status":{ 
      "Application1":"Inactive", 
      "Application2":"Inactive" 
     }, 
     "modifiedBy":"a123456" 
     } 
    ] 
} 

在上面的JSON對象,我試圖檢查如果這兩個應用程序的狀態是無效的,如果是的,我需要刪除的對象我對現在面臨的挑戰甚至閱讀的狀態值也狀態列表是動態的,它可以是一個或多個值任何幫助,將不勝感激讀一個JSONObjects使用javascript一個JSONObjects內/角

+2

你是如何閱讀對象?發佈您的代碼。 – Zee

回答

1

下面是一段代碼,查找所有無效應用指標:

var applications = { 
 
    "APPLICATION_DETAIL":[ 
 
    { 
 
     "Application1":"", 
 
     "Application2":"", 
 
     "status":{ 
 
      "Application1":"Active", 
 
      "Application2":"Inactive" 
 
     }, 
 
     "modifiedBy":"a123453" 
 
     }, 
 
     { 
 
     "Application1":"", 
 
     "Application2":"W", 
 
     "status":{ 
 
      "Application1":"Inactive", 
 
      "Application2":"Inactive" 
 
     }, 
 
     "modifiedBy":"a123456" 
 
     } 
 
    ] 
 
} 
 

 
applications = applications.APPLICATION_DETAIL; 
 
var applicationsNumber = applications.length; 
 
var objectsToRemove = []; 
 

 
for(var appIndex = 0; appIndex < applicationsNumber; appIndex++) { 
 
\t var application = applications[appIndex]; 
 
\t var isActive = false;; 
 
\t 
 
\t var statuses = Object.keys(application.status); 
 
\t var statusesNumner = statuses.length; 
 
\t 
 
\t for(var statusIndex = 0; statusIndex < statusesNumner; statusIndex++) { 
 
\t \t if(application.status[statuses[statusIndex]] === 'Active') { 
 
\t \t \t isActive = true; 
 
\t \t } 
 
\t } 
 
\t 
 
\t if(!isActive) { 
 
\t \t objectsToRemove.unshift(appIndex); 
 
\t } 
 
} 
 

 
console.log(objectsToRemove); 
 

 
var removeObjectsNumber = objectsToRemove; 
 
for(var removeIndex = 0; removeIndex < removeObjectsNumber; removeIndex++) { 
 
    applications = applications.splice(removeIndex, 1); 
 
} 
 

 
console.log(applications);

+0

您的輸出顯示1的狀態對象的關鍵 –

+0

這是工作,但我很抱歉,我是刪除新手如果我使用它使它變爲空白,我嘗試拼接它正在刪除所有內容 – user1767986

+0

我更新了片段。 –

0

首先,我會打印您的應用狀態

複製該代碼並運行你的瀏覽器控制檯

var test ={ 
     "APPLICATION_DETAIL":[ 
     { 
     "Application1":"", 
     "Application2":"", 
     "status":{ 
      "Application1":"Active", 
      "Application2":"Inactive" 
     }, 
     "modifiedBy":"a123453" 
     }, 
     { 
     "Application1":"", 
     "Application2":"W", 
     "status":{ 
      "Application1":"Inactive", 
      "Application2":"Inactive" 
     }, 
     "modifiedBy":"a123456" 
     } 
    ] 
} 
for(var i=0 ; i<test.APPLICATION_DETAIL.length ; i++){ 
    console.log(test.APPLICATION_DETAIL[i].status.Application1); 
    console.log(test.APPLICATION_DETAIL[i].status.Application2); 
} 

現在你可以把如果狀況,並做你的行動

+0

如果你想在if語句中的任何幫助讓我知道 –

+0

,但我不能使用這一行test.APPLICATION_DETAIL [i] .status.Application1作爲application1本身是動態的,它可能有多個應用程序,如狀態對象可以包含儘可能多的6狀態低至1 – user1767986

+0

所以你必須找到使用Object.keys –

相關問題