指向JSON Blob中的其他值以放入数组

时间:2018-10-08 14:00:36

标签: javascript arrays json object

我有一些可以完美运行的代码,它可以将“ counts” json字段中的数据提取到一个数组中。这是代码:

const data = {
  "report": {
    "type": "trended",
    "elements": [{
      "id": "page",
      "name": "Page"
    }],
    "reportSuite": {
      "id": "retail",
      "name": "GlobPROD"
    },
    "period": "Wed.  3 Oct. 2018 - Fri.  5 Oct. 2018",
    "metrics": [{
      "id": "pageviews"
    }],
    "segments": [{
      "id": "s13bb443734ab6a764639ff37",
      "name": "Information"
    }],
    "data": [{
      "name": "Wed.  3 Oct. 2018",
      "year": 2018,
      "month": 10,
      "day": 3,
      "hour": 0,
      "breakdown": [{
        "name": "CATEGORY:>Statement",
        "url": "",
        "counts": ["242"]
      }, {
        "name": "CATEGORY:>Log On",
        "url": "...CheckId.do",
        "counts": ["237"]
      }],
      "breakdownTotal": ["2123"]
    }, {
      "name": "Wed.  3 Oct. 2018 (Hour 1)",
      "year": 2018,
      "month": 10,
      "day": 3,
      "hour": 1,
      "breakdown": [{
        "name": "CATEGORY:>Statement",
        "url": "",
        "counts": ["152"]
      }, {
        "name": "CATEGORY:>Log On",
        "url": "",
        "counts": ["135"]
      }],
      "breakdownTotal": ["1140"]
    }]
  }
}


const label = data.report.data.reduce((r, e) => {
  e.breakdown.forEach(el => {
    let key = "data";
    if (!r[key]) r[key] = []
    r[key].push(...el.counts)
  })
  //return r['data'];
  return r;
}, {})

console.log(label);

但是,我想重复此代码,并同时拉“ day”字段(在上面的示例中为3)。

const label = data.report.data.reduce((r, e) => {
  e.breakdown.forEach(el => {
     let key = "data";
    if(!r[key]) r[key] = []
    r[key].push(...el.day)
  })
  //return r['data'];
  return r;
}, {})

console.log (label);

在上述情况下,我只是将“计数”更改为“天”,但这会产生错误。我认为这是因为JSON中“ day”的级别高于“ counts”的水平,因此我尝试删除了el.day之前的一个周期,但随后出现了以下错误。

Uncaught SyntaxError: Unexpected token.

这很简单,但是我该如何返回日值而不是计数值。

3 个答案:

答案 0 :(得分:2)

要获得成功,您不必遍历breakdown,因为day属性是data.report.data对象的直接属性,它应该很简单:

const label = data.report.data.reduce((r, e) => {
    if( e.hasOwnProperty('day') ) r.push(e.day);

    return r;
}, {})

注意::最好使用.hasOwnProperty()检查对象是否具有指定的属性作为其自身的属性。

答案 1 :(得分:1)

由于day属性是父对象的属性,而不是breakdown子属性的属性,因此请删除内部循环,并传入一个数组作为reducer的初始值:

const data={"report":{"type":"trended","elements":[{"id":"page","name":"Page"}],"reportSuite":{"id":"retail","name":"GlobPROD"},"period":"Wed.  3 Oct. 2018 - Fri.  5 Oct. 2018","metrics":[{"id":"pageviews"}],"segments":[{"id":"s13bb443734ab6a764639ff37","name":"Information"}],"data":[{"name":"Wed.  3 Oct. 2018","year":2018,"month":10,"day":3,"hour":0,"breakdown":[{"name":"CATEGORY:>Statement","url":"","counts":["242"]},{"name":"CATEGORY:>Log On","url":"...CheckId.do","counts":["237"]}],"breakdownTotal":["2123"]},{"name":"Wed.  3 Oct. 2018 (Hour 1)","year":2018,"month":10,"day":3,"hour":1,"breakdown":[{"name":"CATEGORY:>Statement","url":"","counts":["152"]},{"name":"CATEGORY:>Log On","url":"","counts":["135"]}],"breakdownTotal":["1140"]}]}}

const label = data.report.data.reduce((r, e) => {
  if (e.day) r.push(e.day);
  return r;
}, []);

console.log(label);

答案 2 :(得分:1)

如果您的期望值是一个数组,请考虑使用map,尤其是在始终有一个值的情况下:

const data = getData()
const days = data.report.data.map(obj => obj.day);
console.log('days:', days);

function getData() {
  return {
    "report": {
      "type": "trended",
      "elements": [{
        "id": "page",
        "name": "Page"
      }],
      "reportSuite": {
        "id": "retail",
        "name": "GlobPROD"
      },
      "period": "Wed.  3 Oct. 2018 - Fri.  5 Oct. 2018",
      "metrics": [{
        "id": "pageviews"
      }],
      "segments": [{
        "id": "s13bb443734ab6a764639ff37",
        "name": "Information"
      }],
      "data": [{
        "name": "Wed.  3 Oct. 2018",
        "year": 2018,
        "month": 10,
        "day": 3,
        "hour": 0,
        "breakdown": [{
          "name": "CATEGORY:>Statement",
          "url": "",
          "counts": ["242"]
        }, {
          "name": "CATEGORY:>Log On",
          "url": "...CheckId.do",
          "counts": ["237"]
        }],
        "breakdownTotal": ["2123"]
      }, {
        "name": "Wed.  3 Oct. 2018 (Hour 1)",
        "year": 2018,
        "month": 10,
        "day": 3,
        "hour": 1,
        "breakdown": [{
          "name": "CATEGORY:>Statement",
          "url": "",
          "counts": ["152"]
        }, {
          "name": "CATEGORY:>Log On",
          "url": "",
          "counts": ["135"]
        }],
        "breakdownTotal": ["1140"]
      }]
    }
  }
}