Home > front end >  How to splice multiple array elements and insert accordingly?
How to splice multiple array elements and insert accordingly?

Time:11-02

      groupsCall(callType, params) {
        let url = `/api/v3/campaigns/${this.campaignId}/`,
          errorMessage = `could not load the filters`
        url = callType === 'get' ? url   'selected_groups' : url   'update_groups'
        axiosConfig[callType](url, params)
          .then((response) => {
            const data = response.data
            console.log(data)
            const temp = data.groups_with_selected.splice(7,1)(21,3)[0]
            data.groups_with_selected.splice(2,21,0,temp)
            if (isObject(data)) {
              this.setGroupsData(data)
              this.getTotalCount()
              errorMessage = ''
              this.setUpdating(data)
            }
            this.errorMessage = errorMessage
          })
          .catch((error) => {
            errorMessage = `${errorMessage}. ${error}`
            this.errorMessage = errorMessage
            this.updating = false
          })
      },
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

How to splice multiple array elements and insert accordingly?

In the above code, from the response. I am splicing the array element position and placing them in certain position.

i want to splice the (7th position to 2nd position) and (21st position to 3rd position)

But during that process, I couldn't able to re-arrange the order.

CodePudding user response:

Well,

splice(7,1)(21,3)

This code will cause an error. Since Array.prototpy.slice returns a new array.
It would be the same if you would do this:

const a = [1,2,3]
const b = a.splice(1,1);
b(2,1) // b.splice(...) is not a function

EDITED: Maybe there is a faster/better solution but... You can make it more general but for your case only:

const array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21];
const first = array[7];
const second = array[21];

// Add elements on certain indices in array (second and third)
array.splice(2, 0, first, second)

// Remove first from the array (index is 7   2 because we added 2 elements)
array.splice(9, 1)

// Remove 21 from the array (index is 22 - 1 because we added 2 elements and removed 1, not: number 21 is on index 22)
array.splice(21, 1);

CodePudding user response:

data shouldn't be a const since the value is getting updated. Splice can also only be called on one array at a time. If you need to call it on multiple arrays, create a loop or iterate over them.

If you want to inject the current value of the 7th position into the 2nd position... you'd need to do something like...

array.splice(2, 0, array[7])

CodePudding user response:

One way :

 arr = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
 
arr.splice(2, 0, arr[7], arr[21])
 arr.splice(9, 1)
 arr.splice(22, 1)
 console.log(JSON.stringify(arr))
 //[0,1,7,21,2,3,4,5,6,8,9,10,11,12,13,14,15,16,17,18,19,20,] 
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related