简体   繁体   English

如何在ajax响应中获取特定属性?

[英]How to get particular property in ajax response?

This is the ajax response I am getting from the backend. 这是我从后端得到的ajax响应。 I need to search for property "select": true from this response. 我需要从该响应中搜索属性“ select”:true。

Ajax response getting from the backend: 来自后端的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
            }
        ]
    }
 ]
]

This is the expected output: 这是预期的输出:

{
"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
}

Here first I am comparing the project name. 首先,我在这里比较项目名称。 Once I got that, I need to search for "select: true" 知道后,我需要搜索“ 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
      });

Thanks in advance. 提前致谢。

This is approach without external libraries. 这是没有外部库的方法。 To simplify operations on nested arrays flatMap was defined. 为了简化对嵌套数组的操作,定义了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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM