0

I have an array of objects:

const array = [
  { id: 1 },
  { id: 2 },
  { id: 3 },
  { id: 4 }
];

and I need to add another entry to it, but it needs to be placeable within any location in the array. So for example:

array.push({ id: 5, after_id: 2 }); and this should place the new entry between ids 2 and 3. Is there some standard way of doing this?

Xeen
  • 6,955
  • 16
  • 60
  • 111
  • 1
    On further reflection I feel that this is _not_ a duplicate of the question specified. That question looks to insert at a given index, this question looks to insert at a position conditional on the contents of the array. Here's a working example: https://jsfiddle.net/u9rmsvj4/ – OliverRadini Mar 28 '19 at 16:27
  • 5
    `array.splice(array.findIndex(x => x.id === 2), 0, { id: 5 })` (but note if `{ id: 2 }` is not found in the list, it will be added in the second-to-last position). – p.s.w.g Mar 28 '19 at 16:31

1 Answers1

0

@p.s.w.g Has posted what is probably the best solution in a comment already, but I thought I'd post my original solution here as an answer now this is reopened.

You can use some to iterate through the array until the correct index is found, then you can slice the array and insert the item at the relevant index:

const arrayTest = [{
    id: 1
  },
  {
    id: 2
  },
  {
    id: 3
  },
  {
    id: 4
  }
];

const insertAfterId = (array, item, idAfter) => {
  let index = 0;
  array.some((item, i) => {
    index = i + 1;
    return item.id === idAfter
  })

  return [
    ...array.slice(0, index),
    item,
    ...array.slice(index, array.length),
  ];
};

const result = insertAfterId(arrayTest, {
  id: 6
}, 2)
console.dir(result)
OliverRadini
  • 6,238
  • 1
  • 21
  • 46
  • Searching for index can be simplified to: `const index = array.find(e => e.id === idAfter).id` and with object deconstruct it can be `const {id: index} = array.find(e => e.id === idAfter)` – Krzysztof Safjanowski Mar 29 '19 at 10:14
  • @KrzysztofSafjanowski that syntax is much nicer; that'd imply an extra iteration of the array, though? – OliverRadini Mar 29 '19 at 10:31
  • `find` iterates on elements until it finds element - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find - same as `some` method – Krzysztof Safjanowski Mar 29 '19 at 10:57
  • @KrzysztofSafjanowski Sure; but by having _two_ finds surely we're adding an extra iteration? – OliverRadini Mar 29 '19 at 11:07
  • there is only one find - just choice how you want to extract `id` - with object deconstruction or not – Krzysztof Safjanowski Mar 29 '19 at 12:02
  • @KrzysztofSafjanowski ah I see; but this presumes the items have an id which relates to their index, which we do not know to be the case? – OliverRadini Mar 29 '19 at 13:22
  • you are right, there are always some constraints / rules that we have to follow. `arrayTest` can be build by some method - where we will be able to check if id exists or not - if not, developer can throw an error. One of the most important idea is `trash input - trash output` - if we don’t have proper data - how we can manage proper calculation? – Krzysztof Safjanowski Mar 29 '19 at 21:43