0

Is there any function which inserts value at certain index and returns modified array like filter or map?

I already tried array.splice(item, 0, index) but it returns [] instead of modified array

mikolaj semeniuk
  • 2,030
  • 15
  • 31
  • Maybe the OP is going to have a second look at the [syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice#syntax) and the [return value](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice#return_value) of `splice`. – Peter Seliger Mar 08 '21 at 21:53
  • No, there is no in-built function that does this, so you'll have to implement it yourself (`splice` makes this straightforward). – V Maharajh Mar 08 '21 at 21:59
  • 1
    the main question is: do you want a new array or the same array? – Nina Scholz Mar 08 '21 at 22:00
  • Apart from any solutions mentioned in [this question](https://stackoverflow.com/questions/586182/how-to-insert-an-item-into-an-array-at-a-specific-index-javascript), i'd be interested in one, which returns a new array with the element inserted, but doesn't copy the array more than once, doesn't have a function call per element, etc. Just a very low level immutable element insert. I know a `for` loop would do, but adding to native prototypes is kind of a no-no, and a native engine implementation would also be faster anyways. – ASDFGerte Mar 08 '21 at 22:01
  • I want a new array – mikolaj semeniuk Mar 08 '21 at 22:06

5 Answers5

1

You could add a value at an index by returning two values at an index with Array#flatMap

const
    array = [1, 2, 4, 5],
    index = 2,
    newValue = 3,
    newArray = array.flatMap((v, i) => i === index ? [newValue, v] : v);

console.log(...newArray);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

No, but you can add your own function using Array.prototype:

const arr = ["foo", "bar", "baz"]

Array.prototype.spliceReturn = function(idx, val) {
  this.splice(idx, 0, val)
  return this
}
let res = arr.spliceReturn(1, 'biz')
console.log(res)
symlink
  • 11,984
  • 7
  • 29
  • 50
1

You can add to the array using splice and return it:

const insert = (arr, index, val) => {
  arr.splice(index, 0, val);
  return arr;
}

console.log( insert([0,1,3], 2, 2) );
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
1

The next approach handles array items more flexible, be it just a single item or even multiple ones. It meets both acceptance criteria, the one of the OP and the one of ASDFGerte ...

function newArrayFromInsertItems(arr, idx, ...items) {
  arr = Array.from(arr ?? []);
  arr.splice(idx, 0, ...items);
  return arr;
}

const arr = ['foo', 'bar', 'baz'];

console.log(
  "newArrayFromInsertItems(arr, 1, 'buz') ...",
  newArrayFromInsertItems(arr, 1, 'buz')
);
console.log(
  "newArrayFromInsertItems(arr, 1, 'biz', 'buz') ...",
  newArrayFromInsertItems(arr, 1, 'biz', 'buz')
);
console.log(
  "newArrayFromInsertItems(arr, 2, ['bar', 'baz'], 'biz') ...",
  newArrayFromInsertItems(arr, 2, ['bar', 'baz'], 'biz')
);
console.log('arr ...', arr);
.as-console-wrapper { min-height: 100%!important; top: 0; }
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
1
[1, 2, 4].toSpliced(2, 0, 3) // returns a new array, [1, 2, 3, 4].

Check out the documentation for Array.prototype.toSpliced().

Rob Simpson
  • 356
  • 3
  • 6