简体   繁体   English

如何获得包含值的字符串数组中每个字符串名称的最大值?

[英]How do you get the highest value of each individual string name in an array of strings that contain a value?

How would you go about getting the highest number of each color in the following array?您将如何获得以下数组中每种颜色的最高数量?

var array = ["Black 1", "Black 4", "Green 2", "Green 8", "Green 6", "Green 3", "Yellow 2"]

The result array should be:结果数组应该是:

["Black 4", "Green 8", "Yellow 2"]

Any tips or guidance would be extremely appreciated.任何提示或指导将不胜感激。
I was thinking that each value would need to be split between the color and the number but it's not clicking to me how to follow through with that.我在想每个值都需要在颜色和数字之间分开,但我并没有点击我如何跟进。

You could iterate through your array using .reduce() .您可以使用.reduce()遍历您的数组。 As you mentioned, you can use .split() to separate the colour from the number.正如您所提到的,您可以使用.split()将颜色与数字分开。 Once you have both components, you can destructure the array you get from splitting your string to obtain the colour and the number ( score ) into separate variables.一旦您拥有这两个组件,您就可以解构通过拆分字符串获得的数组,以获得colour和数字 ( score ) 到单独的变量中。 Using .reduce() , you can map to a Map , which stores keys as being the colours, and values as being the max score.使用.reduce() ,您可以映射到Map ,它将键存储为颜色,将值存储为最大分数。 In order to get the max score, you can check if the Map already has a number for your colour (indicating it has already been seen), and if it does, grab the max between the current number ( score ) and the current number stored in the Map, otherwise, you can make the Map store the current number score by taking the max between -Infinity and the current score value:为了获得最大分数,您可以检查 Map 是否已经有您的颜色的数字(表明它已经被看到),如果有,则获取当前数字( score )和当前存储的数字之间的最大值在 Map 中,否则,您可以通过取-Infinity和当前分数值之间的最大值使 Map 存储当前数字score

 const array = ["Black 1", "Black 4", "Green 2", "Green 8", "Green 6", "Green 3", "Yellow 2"]; const res = Array.from(array.reduce((map, str) => { const [color, score] = str.split(" "); const maxScore = Math.max(map.get(color) ?? -Infinity, score); return map.set(color, maxScore); }, new Map), entry => entry.join(" ")); console.log(res);

here's my take:这是我的看法:

var array = ["Black 1", "Black 4", "Green 2", "Green 8", "Green 6", "Green 3", "Yellow 2"];
const temp = {};
array.forEach(color => {
  const [name, value] = color.split(' ');
  temp[name] = Math.max(value, temp[name] || 0);
});
const result = Object.entries(temp).map(color => `${color[0]} ${color[1]}`);

Like you said, I split each string into its name and value.就像您说的,我将每个字符串拆分为其名称和值。 Then keep track of the maximums in an object (called temp here).然后跟踪对象中的最大值(此处称为 temp)。 And finally convert to an array.最后转换为数组。

A simple reduce would do the trick.一个简单的reduce就可以解决问题。

Like this (I am using a nested IIFE in this implementation.):像这样(我在这个实现中使用了嵌套的 IIFE。):

 var array = ["Black 1", "Black 4", "Green 2", "Green 8", "Green 6", "Green 3", "Yellow 2"] var result = array.reduce( (acc, cur) => (([k, v]) => ({...acc, [k]: Math.max(v, acc[k] || 0)}) )(cur.match(/(.*)(\\d+)/).slice(1)), // IIFE call {}) console.log(Object.entries(result).map(([k, v]) => `${k}${v}`))

You can first split the string and then using Map to store the largest number and then finally using spread syntax to get the final result.您可以先拆分字符串,然后使用Map存储最大数,最后使用扩展语法来获得最终结果。

 var array = [ "Black 1", "Black 4", "Green 2", "Green 8", "Green 6", "Green 3", "Yellow 2", ]; const mapObj = new Map(); array .map((s) => s.split(" ")) .forEach(([k, v]) => mapObj.set(k, Math.max(mapObj.get(k) ?? Number.MIN_SAFE_INTEGER, v)) ); const result = [...mapObj].map(([k, v]) => `${k} ${v}`); console.log(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Javascript 获得它的最大数字(值)并在 Json 的 ObjectArray 中显示键 [name]? - How do you get the highest number (value) of it and display the key [name] in ObjectArray of Json using Javascript? MongoDB获取每个日期的最高价值并将其放入数组中 - MongoDB Get highest value for each date and put them into an array 如何引用复选框数组名称值? - How do you reference a checkbox array name value? 您如何在javascript中获取名称(文本)的值? - How do you get the value of a name (text) in javascript? 如何从选择器中获取所有元素包含的属性的每个值? - How do you get every value for an attribute that all the elements contain from a selector? 您如何获取HTML和值输出到字符串 - how do you get the html and value output to a string 如何通过值(如果包含)获取键在数组中的索引? - How do you get the index of a key in a array by and if value contains? 将每个字符串是键值对的字符串数组转换为对象 - Converting an array of strings, where each string is a key value pair, to an object 如何使用jquery获得一组元素的最高“左”值? - How do I get the highest “left” value of a group of elements with jquery? 从数组中获取Student的name元素具有最高分值,并在jquery中突出显示此名称 - Get Student's name element has highest point value from an array and highlight this name in jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM