如何通过两项条件找到对象内部的最大值?

时间:2019-04-01 15:10:40

标签: javascript arrays json

我正在尝试通过IDdate获得对象的最高价值。为了更好地解释,这是我的数组:

[  
   {  
      "id":"6",
      "date":"201901",
      "value":"1"
   },
   {  
      "id":"6",
      "date":"201901",
      "value":"12"
   },
   {  
      "id":"6",
      "date":"201901",
      "value":"123"
   },
   {  
      "id":"6",
      "date":"201901",
      "value":"1231"
   },
   {  
      "id":"6",
      "date":"201902",
      "value":"4"
   },
   {  
      "id":"6",
      "date":"201902",
      "value":"45"
   },
   {  
      "id":"5",
      "date":"201902",
      "value":"56"
   },
   {  
      "id":"5",
      "date":"201902",
      "value":"46"
   }
]

我正在尝试获取此回报(通过valueid过滤最高的date):

[  
    {  
      "id":"6",
      "date":"201901",
      "value":"1231"
    },       
    {  
      "id":"6",
      "date":"201902",
      "value":"45"
    },
    {  
      "id":"5",
      "date":"201902",
      "value":"56"
    }       
]

在我的代码中,我使用的是mapfilter,但不起作用:

let resultados = array

array.map(item => {
  resultados.filter(itemFilter => {
    if(item.id == itemFilter.id && item.date == itemFilter.date){
      if(itemFilter.value > item.value){
        return itemFilter.value
      }
    }
  })
})

类似的解决方案是this

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:2)

您可以将Map与想要的键的组合键进行分组。然后取更大的值。

var data = [{ id: "6", date: "201901", value: "1" }, { id: "6", date: "201901", value: "12" }, { id: "6", date: "201901", value: "123" }, { id: "6", date: "201901", value: "1231" }, { id: "6", date: "201902", value: "4" }, { id: "6", date: "201902", value: "45" }, { id: "5", date: "201902", value: "56" }, { id: "5", date: "201902", value: "46" }],
    result = Array.from(data
            .reduce((m, o) => (key =>    
                !m.has(key) || m.get(key).value < o.value
                    ? m.set(key, o)
                    : m
            )(['id', 'date'].map(k => o[k]).join('|')), new Map)
            .values()
        );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

您可以使用Array.prototype.reduce:

yourArray.reduce( (accumulator, currentItem) => {
    var previouslyGreatestItem = accumulator.find(item => item.id == currentItem.id && item.date == currentItem.date);
    if (!previouslyGreatestItem) {
        accumulator.push(currentItem);
    } else if (previouslyGreatestItem.value < currentItem.value) {
        previouslyGreatestItem.value = currentItem.value;
    }
    return accumulator;
}, []);

var yourArray = [{"id":"6","date":"201901","value":"1"},{"id":"6","date":"201901","value":"12"},{"id":"6","date":"201901","value":"123"},{"id":"6","date":"201901","value":"1231"},{"id":"6","date":"201902","value":"4"},{"id":"6","date":"201902","value":"45"},{"id":"5","date":"201902","value":"56"},{"id":"5","date":"201902","value":"46"}];

var result = yourArray.reduce( (accumulator, currentItem) => {
	var previouslyGreatestItem = accumulator.find(item => item.id == currentItem.id && item.date == currentItem.date);
	if (!previouslyGreatestItem) {
		accumulator.push(currentItem);
	} else if (previouslyGreatestItem.value < currentItem.value) {
		previouslyGreatestItem.value = currentItem.value;
	}
	return accumulator;
}, []);

console.log(result);