2017-10-17 80 views
1

這是我從後端得到的ajax響應。我需要從這個響應中搜索屬性「select」:true。如何獲得ajax響應中的特定屬性?

Ajax響應從後端獲得:

[ 
[ 
    { 
     "itemCategory": "Bedroom", 
     "task": "Bedroom", 
     "children": [ 
      { 
       "task": "Smart TV", 
       "price": 300, 
       "leaf": true, 
       "labor": "", 
       "rate": 0, 
       "text": "" 
      }, 
      { 
       "task": "Bed", 
       "price": 500, 
       "leaf": true, 
       "labor": "", 
       "rate": 0, 
       "text": "" 
      } 
     ] 
    }, 
    { 
     "itemCategory": "Kitchen", 
     "task": "Kitchen", 
     "children": [ 
      { 
       "task": "Micro oven", 
       "price": 200, 
       "leaf": true, 
       "labor": "", 
       "rate": 0, 
       "text": "" 
      }, 
      { 
       "task": "Refrigerator", 
       "price": 250, 
       "leaf": true, 
       "labor": "", 
       "rate": 0, 
       "text": "" 
      } 
     ] 
    }, 
    { 
     "itemCategory": "Kitchen", 
     "task": "Kitchen", 
     "children": [ 
      { 
       "task": "Dining Table", 
       "price": 100, 
       "leaf": true, 
       "labor": "", 
       "rate": 0, 
       "text": "" 
      } 
     ] 
    } 
], 
[ 
    { 
     "itemCategory": "Bedroom", 
     "task": "Bedroom", 
     "children": [ 
      { 
       "task": "Smart TV", 
       "price": 300, 
       "leaf": true, 
       "labor": "5", 
       "rate": 4, 
       "text": "", 
       "select": true 
      }, 
      { 
       "task": "Bed", 
       "price": 500, 
       "leaf": true, 
       "labor": "", 
       "rate": 0, 
       "text": "" 
      } 
     ] 
    }, 
    { 
     "itemCategory": "Kitchen", 
     "task": "Kitchen", 
     "children": [ 
      { 
       "task": "Micro oven", 
       "price": 200, 
       "leaf": true, 
       "labor": "", 
       "rate": 0, 
       "text": "" 
      }, 
      { 
       "task": "Refrigerator", 
       "price": 250, 
       "leaf": true, 
       "labor": "4", 
       "rate": 3, 
       "select": true 
      } 
     ] 
    }, 
    { 
     "itemCategory": "Kitchen", 
     "task": "Kitchen", 
     "children": [ 
      { 
       "task": "Dining Table", 
       "price": 100, 
       "leaf": true, 
       "labor": "", 
       "rate": 0, 
       "text": "" 
      } 
     ] 
    } 
], 
[ 
    { 
     "itemCategory": "Bedroom", 
     "task": "Bedroom", 
     "children": [ 
      { 
       "task": "Smart TV", 
       "price": 300, 
       "leaf": true, 
       "labor": "", 
       "rate": 0, 
       "text": "" 
      }, 
      { 
       "task": "Bed", 
       "price": 500, 
       "leaf": true, 
       "labor": "", 
       "rate": 0, 
       "text": "" 
      } 
     ] 
    }, 
    { 
     "itemCategory": "Kitchen", 
     "task": "Kitchen", 
     "children": [ 
      { 
       "task": "Micro oven", 
       "price": 200, 
       "leaf": true, 
       "labor": "", 
       "rate": 0, 
       "text": "" 
      }, 
      { 
       "task": "Refrigerator", 
       "price": 250, 
       "leaf": true, 
       "labor": "", 
       "rate": 0, 
       "text": "" 
      } 
     ] 
    }, 
    { 
     "itemCategory": "Kitchen", 
     "task": "Kitchen", 
     "children": [ 
      { 
       "task": "Dining Table", 
       "price": 100, 
       "leaf": true, 
       "labor": "5", 
       "rate": 4, 
       "text": "", 
       "select": true 
      } 
     ] 
    } 
] 
] 

這是預期的輸出:

{ 
"task": "Smart TV", 
"price": 300, 
"leaf": true, 
"labor": "5", 
"rate": 4, 
"text": "", 
"select": true 
}, 
{ 
"task": "Refrigerator", 
"price": 250, 
"leaf": true, 
"labor": "4", 
"rate": 3, 
"select": true 
}, 
{ 
"task": "Dining Table", 
"price": 100, 
"leaf": true, 
"labor": "5", 
"rate": 4, 
"text": "", 
"select": true 
} 

在這裏首先我比較項目名稱。一旦我得到了,我需要搜索「選擇:真」

var project = "demo"; 
     Ext.Ajax.request({ 
      url: '/common/services/general/basicOperations/getDataByModelUsingGetMethod', 
      method: 'GET', 
      params: { 
       actionId: 'estimatordata', 
       dataJson: '{"aspectType":"Painting Estimator"}' 
      }, 
      success: function(response) { 
       try { 
        var response = response.responseText; 
        var resObj = Ext.decode(response); 
        for (var i = 0; i < resObj.data.length; i++) { 
         if (resObj.data[i].refDataNameName == project) { 
         console.log(JSON.stringify(resObj.data[i]['allGridsData'], null, 4)); 
         } 
        } 
        this.lookupReference('selectionsinquiryDetailGrid').getStore().loadData(
         arr 
       ); 
       } catch (e) { 
        console.log(e); 
       } 
      }, 
      failure: function(response) { 

      }, 
      scope: this 
     }); 

在此先感謝。

+0

你可以使用該 https://lodash.com/docs/4.17.4#filter –

+0

lodash.filter是有可能不使用任何外部庫? –

+0

@ Goutam.A.S是的,你可以使用[過濾方法](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) – George

回答

2

這是沒有外部庫的方法。爲了簡化對嵌套陣列的操作,定義了flatMap

flatMap -> [[1,2], [3,4]] to [1,2,3,4]

const data = [ [ { itemCategory: 'Bedroom', task: 'Bedroom', children: [ { task: 'Smart TV', price: 300, leaf: true, labor: '', rate: 0, text: '' }, { task: 'Bed', price: 500, leaf: true, labor: '', rate: 0, text: '' } ] }, { itemCategory: 'Kitchen', task: 'Kitchen', children: [ { task: 'Micro oven', price: 200, leaf: true, labor: '', rate: 0, text: '' }, { task: 'Refrigerator', price: 250, leaf: true, labor: '', rate: 0, text: '' } ] }, { itemCategory: 'Kitchen', task: 'Kitchen', children: [ { task: 'Dining Table', price: 100, leaf: true, labor: '', rate: 0, text: '' } ] } ], [ { itemCategory: 'Bedroom', task: 'Bedroom', children: [ { task: 'Smart TV', price: 300, leaf: true, labor: '5', rate: 4, text: '', select: true }, { task: 'Bed', price: 500, leaf: true, labor: '', rate: 0, text: '' } ] }, { itemCategory: 'Kitchen', task: 'Kitchen', children: [ { task: 'Micro oven', price: 200, leaf: true, labor: '', rate: 0, text: '' }, { task: 'Refrigerator', price: 250, leaf: true, labor: '4', rate: 3, select: true } ] }, { itemCategory: 'Kitchen', task: 'Kitchen', children: [ { task: 'Dining Table', price: 100, leaf: true, labor: '', rate: 0, text: '' } ] } ], [ { itemCategory: 'Bedroom', task: 'Bedroom', children: [ { task: 'Smart TV', price: 300, leaf: true, labor: '', rate: 0, text: '' }, { task: 'Bed', price: 500, leaf: true, labor: '', rate: 0, text: '' } ] }, { itemCategory: 'Kitchen', task: 'Kitchen', children: [ { task: 'Micro oven', price: 200, leaf: true, labor: '', rate: 0, text: '' }, { task: 'Refrigerator', price: 250, leaf: true, labor: '', rate: 0, text: '' } ] }, { itemCategory: 'Kitchen', task: 'Kitchen', children: [ { task: 'Dining Table', price: 100, leaf: true, labor: '5', rate: 4, text: '', select: true } ] } ] ] 
 

 
function flatMap(arr) { 
 
    return arr.reduce((acc, cur) => { 
 
    acc = acc.concat(cur) 
 
    return acc 
 
    }, []) 
 
} 
 

 
const result = flatMap(flatMap(data).map(x => flatMap(x.children))).filter(x => x.select == true) 
 
console.log(result)

+0

這是行得通的。謝謝你的幫助。 –