2

I would like to optimize the below code as it seems like too many steps. The code does work, however I would like to see if there is a better way of doing it. I basically want to take the first X elements and last X elements from the shipmentTracings array. Is there a way to optimize that part?

Also, I am deriving the elements I want on the left and right side of the array based on numberOfTracings / 2. For an odd number, I am rounding down, but you can see I am using the paddingRight to use the remainder, if any. Is there also a better way to do that?

        const numberOfTracings = environment.shipmentTracingMarkerLimit - this.shipmentTracking.shipmentStops.length;  // i.e. 7
        const paddingLeft = Math.floor(numberOfTracings / 2);
        const paddingRight = numberOfTracings - paddingLeft * 2; //i am doing this because I want to use that extra array spot to be padded.  It 
        const myFirstArr = this.shipmentTracking.shipmentTracings.slice(0, paddingLeft);
        const myFirstArr2 = this.shipmentTracking.shipmentTracings.slice(-(paddingLeft + paddingRight));
        const myFirstArr3 = myFirstArr.concat(myFirstArr2);
        myFirstArr3.forEach((shipmentTracing) => this.createRouteMarkerForTracing(shipmentTracing));
5
  • 1
    Please give example array input and expected output Commented Mar 8, 2019 at 2:57
  • 3
    Would something like this help? [...arr.slice(0, x), ...arr.slice(-x)];? Agree, need expected input/output. Commented Mar 8, 2019 at 2:58
  • this.shipmentTracking.shipmentTracings is just an array of objects. I want to return the same objects from this array. But for example sake lets say it is a simple array as input: var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]. Output should be =["Banana", "Orange", "Apple", "Mango"] (if the paddingleft and right= 2. Commented Mar 8, 2019 at 3:03
  • @ScottRudiger I used your example and that worked. const myFirstArr3 = [...this.shipmentTracking.shipmentTracings.slice(0, paddingLeft), ...this.shipmentTracking.shipmentTracings.slice(-(paddingLeft + paddingRight))]; Commented Mar 8, 2019 at 3:06
  • 1
    Use edit and update the question. Its hard to read code in comments. Commented Mar 8, 2019 at 3:15

1 Answer 1

2

Using your example in the comments, you could use the ES2015 spread operator to do something like this:

const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];

const padding = 2;

const firstAndLast = [...fruits.slice(0, padding), ...fruits.slice(-padding)];

console.log(firstAndLast);

Edit: Update based on comment for calculating the left/right slice based on padding:

const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango", "Dragon Fruit", "Kiwi", "Apricot", "Lime", "Plantain"];

const getFirstAndLast = (arr, padding) => {
  const left = Math.ceil(padding / 2);
  const right = padding - left;
  return [...arr.slice(0, left), ...arr.slice(-right)];
};

// gets first 4 and last 3 given a padding of 7
const firstAndLast = getFirstAndLast(fruits, 7);

console.log(firstAndLast);

Sign up to request clarification or add additional context in comments.

2 Comments

Yes that works! And that answers part of the question. Now that other half of my issue is the calculation of padding. The padding is calculated and is dynamic. For example the total padding for both left and right sides could be 7. So I would want 4 on the left, and 3 from the right. Any suggestions on how to optimize that part?
See the updated answer. Also, please see @Maheer Ali's comments on your question for future reference when using SO going forward. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.