从(JSON)对象获取具有最高价值的特定键

时间:2019-10-18 08:19:11

标签: javascript json

我正在从API响应中获取数据,并试图以最快的速度获得音高的名称。这是API响应的示例。

{
page: 1,
total_pages: 4,
listings: [
    {
        name: "A.J. Burnett",
        pitches: [
            {
                name: "4 Seam FB",
                speed: 96,
                control: 84,
            },
            {
                name: "Knuckle Curve",
                speed: 79,
                control: 74,
            },
            {
                name: "Sinker",
                speed: 95,
                control: 64,
            },
            {
                name: "Changeup",
                speed: 81,
                control: 44,
            }
        ]
    },
    {
        name: "Joe Smitch",
        pitches: [
            {
                name: "4 Seam FB",
                speed: 91,
                control: 82,
            },
            {
                name: "Changeup",
                speed: 69,
                control: 44,
            }
        ]
    },
]
}

这是我尝试过的:

itemSet.forEach( (item) => {
    let fastestPitch = Object.keys(item.pitches).reduce((a, b) => {
        item.pitches[a] > item.pitches[b] ? item.pitches[a].name : item.pitches[b].name
     });
});

但是,它总是返回数组中LAST音高的名称。我试图以最快的速度返回音高。

编辑:我也尝试了以下方法,但返回错误。

itemSet.forEach( (item) => {
    let fastestPitch = Object.keys(item.pitches).reduce((a, b) => {
        item.pitches[a].speed > item.pitches[b].speed ? item.pitches[a].name : item.pitches[b].name
     });
});

错误:

(node:80698) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'speed' of undefined

3 个答案:

答案 0 :(得分:1)

要提取最快的每个,您可以Array#map listings中的每个条目,然后Array#reduce pitches中的let data = { page: 1, total_pages: 4, listings: [{ name: "A.J. Burnett", pitches: [{ name: "4 Seam FB", speed: 96, control: 84, }, { name: "Knuckle Curve", speed: 79, control: 74, }, { name: "Sinker", speed: 95, control: 64, }, { name: "Changeup", speed: 81, control: 44, } ] }, { name: "Joe Smitch", pitches: [{ name: "4 Seam FB", speed: 91, control: 82, }, { name: "Changeup", speed: 69, control: 44, } ] }, ] }; let fastestPitches = data.listings.map(obj => { return obj.pitches.reduce((best, current) => { return best.speed > current.speed ? best : current }, {}).name }); console.log(fastestPitches)条目,如下所示:

best

请注意,当您减少时,第一个参数(在这种情况下为name)是 previous 回调的结果。因此,如果仅返回名称,您将不会知道它的速度。因此,您遍历并比较速度,然后返回更好的整个对象。完成后,您将获得结果的MONTH()

答案 1 :(得分:1)

您可以执行以下操作:

const data = {
  page: 1,
  total_pages: 4,
  listings: [{
      name: "A.J. Burnett",
      pitches: [{
          name: "4 Seam FB",
          speed: 96,
          control: 84,
        },
        {
          name: "Knuckle Curve",
          speed: 79,
          control: 74,
        },
        {
          name: "Sinker",
          speed: 95,
          control: 64,
        },
        {
          name: "Changeup",
          speed: 81,
          control: 44,
        }
      ]
    },
    {
      name: "Joe Smitch",
      pitches: [{
          name: "4 Seam FB",
          speed: 91,
          control: 82,
        },
        {
          name: "Changeup",
          speed: 69,
          control: 44,
        }
      ]
    },
  ]
}

const fastesPitches = data.listings.map(({ pitches }) => {
  return pitches.reduce((a, c) => c.speed > a.speed ? c : a).name;
});

console.log(fastesPitches);

答案 2 :(得分:1)

您可以采用一种完整的动态方法来查找任何深度,然后从嵌套最多的对象中返回具有所需最高属性的对象。

function getHighest(object, key) {
    return Object.values(object).reduce((r, o) => {
        if (!o || typeof o !== 'object') return r;
        if (key in o && (!r || r[key] < o[key])) return o;
        var temp = getHighest(o, key);
        if (temp && (!r || r[key] < temp[key])) return temp;
        return r;
    }, undefined);
}

var data = { page: 1, total_pages: 4, listings: [{ name: "A.J. Burnett", pitches: [{ name: "4 Seam FB", speed: 96, control: 84 }, { name: "Knuckle Curve", speed: 79, control: 74 }, { name: "Sinker", speed: 95, control: 64 }, { name: "Changeup", speed: 81, control: 44 }] }, { name: "Joe Smitch", pitches: [{ name: "4 Seam FB", speed: 91, control: 82 }, { name: "Changeup", speed: 69, control: 44 }] }] },
    highest = getHighest(data, 'speed');

console.log(highest.name);
console.log(highest);