USEFULL FUNCTION FOR ARRAY - JS

Sort Array by element length

//Declare an Array
const fruits = ['banana', 'apple', 'cherry'];

//Declare sortBylength function
function sortBylength(str1,str2)
{
    return str1.length - str2.length;
}

//Call the function sort()
const sortedFruits = fruits.sort(sortBylength);

Simple way to keep a tag from an array of payload (.map)

When you store several payload in a buffer, they are often store on a array format

In this simple example, we have 7 payloads stored in the array and 2 tags in each payload

Keep the tag1 in a new array :

const myArray = [{"tag1" : "toto", "tag2" : 2} ,
                 {"tag1" : "tata", "tag2" : 5},
                 {"tag1" : "tutu", "tag2" : 8},
                 {"tag1" : "tata", "tag2" : 4},
                 {"tag1" : "toto", "tag2" : 4},
                 {"tag1" : "toto", "tag2" : 9},
                 {"tag1" : "tete", "tag2" : 10}]

const ArrayTag1 = myArray.map(line => line.tag1)

/*ArrayTag1 =[
  'toto', 'tata',
  'tutu', 'tata',
  'toto', 'toto',
  'tete']*/

Remove duplicates in an array

//Array with a duplicate value
const myArray = [
  'toto', 'tata',
  'tutu', 'tata',
  'toto', 'toto',
  'tete'
]

let myArrayWithoutDuplicate = [...new Set(myArray)];
//myArrayWithoutDuplicate = [ 'toto', 'tata', 'tutu', 'tete' ]

Simple way to sum a tag from an array of a payload (.reduce())

const ArrayOfPayload = [
  {
    id: 10,
    name: "Poe Dameron",
    years: 14,
  },
  {
    id: 2,
    name: "Temmin 'Snap' Wexley",
    years: 30,
  },
  {
    id: 41,
    name: "Tallissan Lintra",
    years: 16,
  },
  {
    id: 99,
    name: "Ello Asty",
    years: 22,
  }
];

//Simple way to sum a tag from an array of a payload
//Method "reduce()" move from a payload to another in the array, and add in the parameter "accumulator" the value of each tag "years" of each payload (line) of the array
const totalYears = payloadArr.reduce((accumulator, line) => accumulator + line.years, 0);

//totalYears = 82

Sum tag1 and group by tag2 (.reduce())

Use case : We want to sum the tag "value" and group by "name" and return the result

const ArrayOfPayload = [{"name" : "toto", "value" : 2} ,
                        {"name" : "tata", "value" : 5},
                        {"name" : "tutu", "value" : 8},
                        {"name" : "tata", "value" : 4},
                        {"name" : "toto", "value" : 4},
                        {"name" : "toto", "value" : 9},
                        {"name" : "tete", "value" : 10}];

const sumValueGrpByName = ArrayOfPayload.reduce(function(acc,obj){
    if(!acc[obj.name])
    {
      acc[obj.name] = 0;
    }
      acc[obj.name] = acc[obj.name] + obj.value;
  return acc;
},{});

//sumValueGrpByName  = { toto: 15, tata: 9, tutu: 8, tete: 10 }

Transform an object into an array (Object.entries)

const myObject = { apple : 4, banana : 2, tomato : 6, peach : 3} 

const myArray= Object.entries(myObject);
// myArray = [ [ 'apple', 4 ], [ 'banana', 2 ], [ 'tomato', 6 ], [ 'peach', 3 ] ]

Simple way to filter on a tag and return the result in an array (.filter())

const ArrayOfPayload = [
  {
    id: 2,
    name: "PM6",
    Country: "Germany",
  },
  {
    id: 8,
    name: "PM2",
    Country: "France",
  },
  {
    id: 40,
    name: "PM8",
    Country: "USA",
  },
  {
    id: 66,
    name: "PM4",
    Country: "France",
  }
];

//Keep only line (payload) who "Country" tag equals "France"
var PM_from_France = ArrayOfPayload.filter(line => line.Country === "France");

/*PM_from_France = [
  { id: 8, name: 'PM2', Country: 'France' },
  { id: 66, name: 'PM4', Country: 'France' }
]*/

Was this article helpful?

Powered by Zendesk