简体   繁体   中英

Filtering a nested array - javascript

I have a nested array and each array has a string and an integer, the strings in some of the array are the same but i want to filter the array so that it only contains nested arrays with unique names and that have the highest values. Here's an example of what i have and what i want:

[['a', 1],['a', 2],['a', 3],['b',2],['b',5]]

what i want to do is filter so that it contains this:

[['a', 3],['b', 5]]

I originally tried doing this with a for loop and an if statement, then a for loop and a while statement, when i looked at filtering but i'm not sure how to implement it where it will keep the string with the highest value, please help!!!!

You could take a Map for grouping by the first element and get the maximum values by checking stored values.

 var array = [['a', 1], ['a', 2], ['a', 3], ['b', 2], ['b', 5]], result = Array.from( array.reduce((m, [k, v]) => m.set(k, m.has(k) ? Math.max(v, m.get(k)) : v), new Map) ); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

If you like to keep the original arrays, you could store the array instead of the value and take later only the values of the map.

 var array = [['a', 1], ['a', 2], ['a', 3, 'bar'], ['b', 2], ['b', 5, 'foo']], result = Array.from(array .reduce( (m, a) => m.has(a[0]) && m.get(a[0])[1] > a[1] ? m : m.set(a[0], a), new Map ) .values() ); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Here is a solution using reduce and Object.keys and map .

 const data = [['a', 1],['a', 2],['a', 3],['b',2],['b',5]]; //in reducer we work with object instead of array //efficient since we avoid doing an extra loop const result = data.reduce((acc, cur)=>{ //create variables from current array (ex: ['a', 1]) const letter = cur[0]; const value = cur[1]; //Acc (accumulator) holds the highest values (ex {a: 1, b: 2} ) //If the letter doesn't yet exist or if the cur value is higher we update the acc if(!acc[letter] || acc[letter] < value){ acc[letter] = value; } return acc; }, {}); //Not in the correct format, so we transform the result into the requested format const final = Object.keys(result).map(key=>[key, result[key]]); console.log(final); 

const data = [['a', 1],['a', 2],['a', 3],['b',2],['b',5]]
const buf = {}
data.map(arr => {
    if(!buf[arr[0]] || buf[arr[0]] < arr[1])
        buf[arr[0]] = arr[1]
})
const result = Object.keys(buf).map(k => [k, buf[k]]);
console.log(result)
const obj = [['a', 1],['a', 2],['a', 3],['b',2],['b',5]].reduce((res, arr) => {
    res[arr[0]] = !res[arr[0]] ? arr[1] : Math.max(res[arr[0]], arr[1])
    return res
}, {})
const result = Object.keys(obj).map((key) => [key, obj[key]])
const data = [['a', 1],['a', 2],['a', 3],['b',2],['b',5]];
var result = data.sort(function(a,b){
    return Math.max(b[1]-a[1]);
});
var new_data= result.slice(0,2);
console.log(new_data.reverse());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM