Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
Javascript - how to remove objects within an object of arrays of objects by key value

I'm trying to figure out how to remove objects within an array based on a key value.

Currently, the object looks something like this:

const myArray = [
      {
      firstname: 'John',
      lastName: 'Doe',
      sortOrder: 22
    },
    {
      firstname: 'Jane',
      lastName: 'Doe',
      sortOrder: 11
    },{
      firstname: 'Jen',
      lastName: 'SomeLastName',
      sortOrder: 33
    },
    {
      firstname: 'ExampleFirstName',
      lastName: 'ExampleLastName',
      sortOrder: 12
    }
];

I'm hoping to reduced the array by a specified amount, removing objects with the highest sort order, so that the function removing objects would return:

const myArray = [
  {
      firstname: 'Jane',
      lastName: 'Doe',
      sortOrder: 11
    },{
      firstname: 'ExampleFirstName',
      lastName: 'ExampleLastName',
      sortOrder: 12
    }
];

My current function looks like this, but doesn't remove anything:

 reduceArray = (myArray, amount) => {

    let keys = Object.keys(myArray);
    let keyLength = keys.length;
    let orderArray = [];
    let i = 0,
      j = 0;
    for (i = 0; i < keyLength; i++) {
      let subArray = myArray[keys[i]];
      let subLength = subArray.length;
      for (j = 0; j < subLength; j++) {
        orderArray.push(subArray[j].sortOrder);
      }
    }
  
    let totalLength = orderArray.length;
    for (i = 0; i < totalLength - 1; i++)
      for (j = i + 1; j < totalLength; j++)
        if (orderArray[i] < orderArray[j]) {
          let temp = orderArray[i];
          orderArray[i] = orderArray[j];
          orderArray[j] = temp;
        }
  
    orderArray.splice(amount, totalLength - amount);
    let deletedAmount = 0;
    for (i = 0; i < keyLength; i++) {
      let subArray = myArray[keys[i]];
      let subLength = subArray.length;
      for (j = 0; j < subLength; j++) {
        if (orderArray.includes(subArray[j].sortOrder)) {
          subArray.splice(j, 1);
          j--;
          subLength--;
          deletedAmount++;
          if (deletedAmount == amount) break;
        }
      }
      if (deletedAmount == amount) break;
    }
  
    return myArray;
  }

And I'm calling it like this:

const reducedArray = reduceArray(myArray, 2);

I just returns my same array without anything removed.

Any idea on what I might be doing wrong?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!