如何获取具有相同名称和ID的对象的最大值

我有一个如下所示的对象数组


   [ 

            {value: 1, id: 1, name: "x"},

            {value: 5, id: 1, name: "x"},

            {value: 1, id: 1, name: "y"},

            {value: 8, id: 1, name: "y"},

            {value: 1, id: 2, name: "x"},

            {value: 3, id: 2, name: "x"},

            {value: 1, id: 2, name: "y"},

            {value: 4, id: 2, name: "y"}

     ]

我想获取具有相同“名称”和“id”的最大值的对象并将其推入一个新数组中,预期输出如下:


   [

     {value: 5, id: 1, name: "x"},

     {value: 8, id: 1, name: "y"},

     {value: 3, id: 2, name: "x"},

     {value: 4, id: 2, name: "y"},

   ]

谢谢


繁星淼淼
浏览 163回答 2
2回答

catspeake

你可以使用reduce方法来做到这一点,const data = [&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {value: 1, id: 1, name: "x"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {value: 5, id: 1, name: "x"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {value: 1, id: 1, name: "y"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {value: 8, id: 1, name: "y"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {value: 1, id: 2, name: "x"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {value: 3, id: 2, name: "x"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {value: 1, id: 2, name: "y"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {value: 4, id: 2, name: "y"}&nbsp; &nbsp; &nbsp;]const res = data.reduce((prev, curr) => {&nbsp; &nbsp;const index = prev.findIndex((item) => item.id === curr.id && item.name === curr.name);&nbsp; &nbsp;if(index > -1) {&nbsp; &nbsp; &nbsp; const obj = prev[index];&nbsp; &nbsp; &nbsp; if(obj.value < curr.value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prev[index] = {...obj, value: curr.value};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return prev;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}&nbsp; &nbsp;prev.push(curr);&nbsp; &nbsp;return prev;}, []);console.log(res);

幕布斯6054654

Reduce用于从数组中的所有项返回一个新值,该值基本上是累加器(与先前的值相加)。在这里我们可以使用它使用特定键对项目进行分组。正如您所写,您希望项目显示具有相同 id 和名称的最大值的记录,这些值可以作为键(让我们将它们视为该对象的复合私钥)。在每次迭代中,我们检查是否已经有一个带有该键的对象添加到列表中,如果没有,我们添加我们现在所在的对象(在迭代期间),或者如果它的值小于我们当前所在的对象。如果该值较小,我们将使用当前对象覆盖该对象。最后,我们使用 JS Object.values方法去除键并仅返回对象的值。const list = [   {value: 1, id: 1, name: "x"},  {value: 5, id: 1, name: "x"},  {value: 1, id: 1, name: "y"},  {value: 8, id: 1, name: "y"},  {value: 1, id: 2, name: "x"},  {value: 3, id: 2, name: "x"},  {value: 1, id: 2, name: "y"},  {value: 4, id: 2, name: "y"} ];const groupedResults = list.reduce((result, currentObject) => {  const currentKey = currentObject.id + currentObject.name;  if (!result[currentKey] || result[currentKey].value <   currentObject.value) { /* Here we check if object with certain key was assigned to previously or if it was is the value smaller than of the object that we are currently seeing */    result[currentKey] = Object.assign({}, currentObject) //We need to do copy of the object (it can be also done using object destructuring) in order to have a new object that will not be bound by reference with the original one  };  return result;}, {});const requestedList = Object.values(groupedResults);console.log(requestedList)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript