繁体   English   中英

使用 JavaScript 映射和缩减 JSON 对象

[英]Map and Reduce a JSON Object with JavaScript

考虑下面这个对象:

    {
    "_items": [
        {
            "_id": "player_1",
            "score": {
                "a": -4.74,
                "b": 0.71,
                "c": -4.04,
                "d": 3.37,
                "e": 0.22,
                "f": 1.09,
                "g": -2.17              
            }
        }
    ]
}

我会映射和减少并生成一个仅包含分数对象的新对象:

{
    "a": -4.74,
    "b": 0.71,
    "c": -4.04,
    "d": 3.37,
    "e": 0.22,
    "f": 1.09,
    "g": -2.17
}

我在想这样的事情可能会朝着正确的方向发展,但似乎并没有达到我的预期:

this.service.getscore()
  .map(res => res.json())
  .map(v => v._items[0].score)
  .subscribe(data => { this.sc = data });

console.log(this.sc); 会给我与第一个 json 相同的结果。

虽然我认识到在服务器端执行此操作可能更好更容易,但在我的情况下这是不可能的。 我想知道我正在尝试做的事情是否可以在客户端使用 JavaScript 完成。 有什么建议吗?

你可以试试我下面的代码:

var item = {
  "_items": [{
      "_id": "player_1",
      "score": {
        "a": -4.74,
        "b": 0.71,
        "c": -4.04,
        "d": 3.37,
        "e": 0.22,
        "f": 1.09,
        "g": -2.17
      }
    },
    {
      "_id": "player_2",
      "score": {
        "a": -4.74,
        "b": 0.71,
        "c": -4.04,
        "d": 3.37,
        "e": 0.22,
        "f": 1.09,
        "g": -2.17
      }
    }
  ]
};
let arrayScores = item._items.map(el => el.score);
console.log(arrayScores);

有 arrayScores 值:

[{
  a: -4.74,
  b: 0.71,
  c: -4.04,
  d: 3.37,
  e: 0.22,
  f: 1.09,
  g: -2.17
}, {
  a: -4.74,
  b: 0.71,
  c: -4.04,
  d: 3.37,
  e: 0.22,
  f: 1.09,
  g: -2.17
}]

有意义吗?

越简单越好?:

    var json = [{
        "_items": [
            {
                "_id": "player_1",
                "score": {
                    "a": -4.74,
                    "b": 0.71,
                    "c": -4.04,
                    "d": 3.37,
                    "e": 0.22,
                    "f": 1.09,
                    "g": -2.17
                }
            }
        ]
    }, {
        "_items": [
            {
                "_id": "player_2",
                "score": {
                    "a": -4.74,
                    "b": 0.71,
                    "c": -4.04,
                    "d": 3.37,
                    "e": 0.22,
                    "f": 1.09,
                    "g": -2.17
                }
            }
        ]
    }];
var scores = [];
for (var i = 0, var j = json.length; i <= j; i++) {
    scores.push(json[i]._items[0].score);
}

正如 Maria Ines Parnisari 所提到的,您可以通过进入您收到的 JSON 来准确地提取您想要的数据。 所以我们只提取score的内容并赋值给一个对象:

服务:

getscore() {
  return this.http.get('src/data.json')
    .map(res => res.json()._items[0].score) // let's get the content of 'score'
}

成分:

ngOnInit() {
  this.service.getscore()
    .subscribe(data => {
      this.sc = data;
    })
}

演示

工作演示

 this.sc = { "_items": [ { "_id": "player_1", "score": { "a": -4.74, "b": 0.71, "c": -4.04, "d": 3.37, "e": 0.22, "f": 1.09, "g": -2.17 } } ] }; console.log(this.sc._items[0].score);

暂无
暂无

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

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