You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在JavaScript对象数组中查找make最大值对应的完整对象

获取make值等于最大值的对象

你已经算出了make属性的最大值mm,接下来用数组的find()方法就能直接拿到目标对象:

// 匹配make等于最大值的对象
const targetCar = myArr.find(car => car.make === mm);
console.log(targetCar);
>>> { make: 3, model: 'c' }

额外说明:

  • find()会返回数组中第一个符合条件的元素,刚好适配你的场景(当前最大值唯一)。
  • 如果后续存在多个make等于最大值的对象,想要获取所有匹配项,改用filter():
const targetCars = myArr.filter(car => car.make === mm);
console.log(targetCars);
>>> [ { make: 3, model: 'c' } ]

优化初始对象数组生成代码(可选):

你原来的循环可以简化成更简洁的写法,用map()一步生成对象数组:

const arr1 = [1, 2, 3];
const arr2 = ['a', 'b', 'c'];
const myArr = arr1.map((make, index) => ({ make, model: arr2[index] }));

这样无需手动创建对象和循环,代码更简洁易读。

内容的提问来源于stack exchange,提问作者Gianni Spear

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 20:06:22