簡體   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