Filtering a nested array - javascript

javascript filter array of objects by key
javascript filter array multiple values
javascript filter nested array of objects
filter array of arrays javascript
javascript array.filter multiple arguments
filter two array of objects javascript
filter two arrays javascript
javascript filter object

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)
.as-console-wrapper { max-height: 100% !important; top: 0; }

This tutorial shows you how to use the JavaScript array filter method to filter elements in an array based on a specified condition. Nested loops should do the trick. Place a for loop inside another for loop. The outer loop iterates though the items in the outer array; the inner loop iterates through the items in the inner arrays. To include a block of code in your comment, surround it with <pre> </pre> tags. You can include smaller code snippets inside some normal text

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]]);


The filter() method creates a new array with all elements that pass the const nested = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; let flat = nested.reduce((acc,  filter() calls a provided callback function once for each element in an array, and constructs a new array of all the values for which callback returns a value that coerces to true. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.

const data = [['a', 1],['a', 2],['a', 3],['b',2],['b',5]]
const buf = {} => {
    if(!buf[arr[0]] || buf[arr[0]] < arr[1])
        buf[arr[0]] = arr[1]
const result = Object.keys(buf).map(k => [k, buf[k]]);

Help with accessing/filtering a nested value in react state array Can you post a full example JS object, not the thing that console log prints? Accepting a function as an argument, using .filter() will return a new array with all elements that return true. Product table design Before diving straight into explaining, let's review the

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]])

And one of the most common tasks is taking an array and filtering it down to a subset of the elements it contains. A straightforward way of doing the filtering  Try the below: function getResult(filterBy, objList) { return objList.hightlights.filter(function(obj) { return obj.queries.some(function(item){ return item.indexOf

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);

If you truly are trying to filter the array based on the presence of the field on the objects in the array, then that may look like: var searchField = 'abc_name'; // O(n) iteration with O(1) field lookup in filter callback var filteredData = datas. filter( data => searchField in data. fields ); JavaScript arrays are used to store multiple values in a single variable. An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: However, what if you want to loop through the cars and find a specific one?

JavaScript Array API - map, reduce, iterations, iterables and more! All you have to do is pass a filtering function, which, given current element's Its rules are simple - it simply flattens your nested array by specified depth  Using Filter Array.Filter returns a new array of elements which pass the given test functions. It loops over all the elements of the source array, calling test function for each item, if test

Filter an array based on the values of one of its nested arrays in vue? posts : [ { title: post_ab, category_array: [ { id: 1, slug: category - a }, { id: 2, slug: When they say “You don't know Javascript” it's me they had in mind… by Alex Permyakov. How to simplify your codebase with map(), reduce(), and filter() in JavaScript Photo by Anders Jildén on Unsplash. When you read about Array.reduce and how cool it is, the first and sometimes the only example you find is the sum of numbers.

Filtering Nested Arrays — However, we have to first reduce the array and then filter it by our desired value. const characters = [ [ { type : 'Hero',  The flat () method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. { {EmbedInteractiveExample ("pages/js/array-flatten.html")}} The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https

  • please add what you have tried.
  • it works!! thanks!! would you mind explaining it to me in depth?
  • I'd recommend marking Nina's solution as the right answer. It's much better than mine.
  • please explain your answer
  • @Rafael iterate data using map and store key and max value to buf object. after that, construct proper result array from buf object. since other answers are using reduce, my intent was to show different solution.
  • posting code without explanation is usually not helpful
  • Thanks, I will keep that in mind. I thought "Here is solution" or "You can do this way" kind of explanation is unnecessary
  • As you most likely know, posting only code without an explanation of how your code works is not good practice. Giving a brief summary of the code would help learners (like the question's author) a lot.