Moving an array of elements to another array index

Not sure if this might be a duplicate, but I'm having some problems trying to come up with a better way to move an element to an array filled with arrays of elements.

For instance:

var foo = [
    [ {obj: 1}, {obj: 2}, {obj: 3}, {obj: 4} ],
    [ {obj: 5}, {obj: 6}, {obj: 7}, {obj: 8} ], 
    [ {obj: 9}, {obj: 10}, {obj: 11}, {obj: 12} ]
];

If I delete one element specified by arrayIndex, it will delete that element and then move all the controls to the corresponding array. For example, if I remove obj 3, the result is:

var arrayIndex = 0;
var objIndex = 2;

var bar = foo[arrayIndex].splice(objIndex, 1);

Result:

bar = [
    [ {obj: 1}, {obj: 2}, {obj: 4}, {obj: 5} ],
    [ {obj: 6}, {obj: 7}, {obj: 8}, {obj: 9} ], 
    [ {obj: 10}, {obj: 11}, {obj: 12} ]
];

Another example may be as shown removing obj 8:

var arrayIndex = 1;
var objIndex = 3;

var bar = foo[arrayIndex].splice(objIndex, 1);

Result:

bar = [
    [ {obj: 1}, {obj: 2}, {obj: 3}, {obj: 4} ],
    [ {obj: 5}, {obj: 6}, {obj: 7}, {obj: 9} ], 
    [ {obj: 10}, {obj: 11}, {obj: 12} ]
];

The problem for me is shifting all processing elements to the correct position of the array. Also, I would like to delete an empty array. Where the length of foo will decrease. foo will also be mutated.

Here is my jsfiddle attempt: https://jsfiddle.net/mLw8kncn/1/

Any help would be appreciated.

+4
3

1D 2D. .

var foo = [ {obj: 1}, {obj: 2}, {obj: 3}, {obj: 4},
    {obj: 5}, {obj: 6}, {obj: 7}, {obj: 8},
    {obj: 9}, {obj: 10}, {obj: 11}, {obj: 12} ];

function remove(arrayIndex, objIndex) {
    var realIndex = arrayIndex * 4 + objIndex;
    foo.splice(realIndex, 1);
}

.

function remove(arrayIndex, objIndex) {
    foo[arrayIndex].splice(objIndex, 1);

    for (var i = arrayIndex + 1; i < foo.length; i++) {
        var obj = foo[i].shift();
        foo[i - 1].push(obj);
    }

    if (foo[foo.length - 1].length <= 0) {
        foo.pop();
    }
}

.

+3

, 1 ( ), splice , , 2D- .

, splice / .

, , :

function splice2d(a, start, deleteCount /*, item1, item2, ...*/){
    var flat = [], sizes = [];
    // Make a flat array, keeping record of subarray sizes
    while (a.length) {
        sizes.push(a[0].length);
        flat = flat.concat(a.shift());
    };
    // Apply original splice to flat array
    [].splice.apply(flat, [].slice.call(arguments, 1));
    // Reconstruct 2D array again based on sizes
    while (sizes.length) {
        a.push(flat.splice(0, sizes.shift()));
    }
    return a;
}

// Sample data
var foo = 
    [[{obj: 1}, {obj: 2}, {obj: 3}, {obj: 4}],
    [{obj: 5}, {obj: 6}, {obj: 7}, {obj: 8}], 
    [{obj: 9}, {obj: 10}, {obj: 11}, {obj: 12}]]

// Mutate
splice2d(foo, 2, 1);
// Output result
console.log(foo);
Hide result
0

, Array.prototype.reduce() . ,

var        foo =  [[{obj: 1}, {obj: 2}, {obj: 3}, {obj: 4}],
                   [{obj: 5}, {obj: 6}, {obj: 7}, {obj: 8}], 
                   [{obj: 9}, {obj: 10}, {obj: 11}, {obj: 12}]
                  ],
removeAndShift = (a,ai,oi) => ai == a.length-1 ? a[ai].splice(oi,1)
                                               : a.reduce((p,c,i,a) => { if (i == ai+1) {
	                                                                    p.splice(oi,1);
	                                                                    p.push(c.shift());
                                                                         }
                                                                         i > ai+1 && p.push(c.shift());
                                                                         return c;
                                                                       });
 removeAndShift(foo,1,3);
 console.log(foo);
Hide result

Please note that we are not doing anything, but simply connecting the element to be deleted if the array element to delete the element is at the very end.

0
source

All Articles