1

I'm new to js and using splice to remove an element from an array, however I always get the removed array. ex:

let myFish = ['angel', 'clown', 'mandarin', 'sturgeon']
myFish.splice(-1,1) // goves o/p ['sturgeon']

however I want the remaining array element expected o/p:

['angel', 'clown', 'mandarin']

how to achieve this?

11
  • 1
    The non-removed are in your array, myFish, here. Commented Sep 22, 2020 at 21:32
  • 2
    splice changes the Array, that's why it returns that which has been removed. Commented Sep 22, 2020 at 21:32
  • 2
    slice, on the other hand, gives you part of the array without changing the array. Commented Sep 22, 2020 at 21:33
  • @iAmOren wouldn't one answer be better then three comments? :-) Commented Sep 22, 2020 at 21:35
  • so this is basically a string that I have: str= "Hello_Worksl_SHARED" and I'm doing split() and then splice- str.split("_").splice(-1,1) however if I need to access the non-removed array, I need to do 2 steps: let a = str.split("_") then a.splice(-1,1) and then access a. is there a way I can do everything in 1 step? Commented Sep 22, 2020 at 21:37

1 Answer 1

3

var str="Hello_Worksl_SHARED", splitStr, strMinusEnd;
strMinusEnd=(splitStr=str.split("_")).slice(0,-1);

console.log("str="+str);
console.log("splitStr="+splitStr);
console.log("strMinusEnd="+strMinusEnd);


Before your comment with extra info:

The non-removed are in your array, myFish.

splice changes the Array, that's why it returns that which has been removed.

slice, on the other hand, gives you part of the array without changing the array.


I'm not sure what you want, so here is everything:

var str="Hello_Worksl_SHARED";
var arrFromStr, arrMinusEnd, endItem;

[arrFromStr, arrMinusEnd, endItem] = 
  str.split("_")
  .reduce(
    (acc, cv, idx, arr) => {
      if(idx<arr.length-1) acc[1].push(cv);
      else {
        acc[0]=arr;
        acc[2]=cv;
      };
      return acc;
    },
    [null,[],[]]
  )
;

console.log("str=\""+str+"\"");
console.log("arrFromStr=");
console.log(arrFromStr);
console.log("arrMinusEnd=");
console.log(arrMinusEnd);
console.log("endItem=\""+endItem+"\"");
.as-console-wrapper { max-height: 100% !important; top: 0; }

We're using destructuring of an array.
Right-hand-side of the = returns an array of 3 elements.
Each of those are respectfully(?) assigned to the 3 variables on the left-hand-side of that =.

The reduce is spread over multiple lines for clarity.

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

2 Comments

If you want end and start instead of full sliced array, you can use slightly modified (but using same approach) var str="Hello_Worksl_SHARED", end, start; end=(start=str.split("_")).splice(-1,1); Maybe would be worth adding to the answer
@JanStránský, as more details about the desired output are unveiling, modifications are required...

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.