9

I have an array of objects. Then I want to add another object and stick it to the one that already existed in my array. Which means the index of the new object should be larger by one in relation to my already existed object and rest of the element's indexes should be incremented by one.

For example:

  1. I have array of 6 elements
  2. My new object is stick to existed object with index = 2
  3. New Object enters into an array with index = 3 and all objects with indexes previously greater then 2 now get one place higher

I tried to split my array into two starting from index = 2, push my new element, and then join and again but my code do not work well.

for (var i in myArray) {
  if (myArray[i].name === inheritedRate.inherit) {
    var tempArr = [];
    for (var n = i; n < myArray.length; n++) {
      tempArr.push($scope.myArray[n]);
      myArray.splice(n, 1);
    }
    myArray.push(inheritedRate);
    myArray.concat(tempArr);
  }
}

in other words I have an array which looks like this:

myArray = [
  {name: "not selected"},
  {name: "not selected"},
  {name: "selected"},
  {name: "not selected"},
  {name: "not selected"},
  {name: "not selected"},
]

And I want to put there an external element to make it look this:

myArray = [
  {name: "not selected"},
  {name: "not selected"},
  {name: "selected"},
  {name: "new element"}, // inserted
  {name: "not selected"},          
  {name: "not selected"},
  {name: "not selected"},
]
georg
  • 211,518
  • 52
  • 313
  • 390
andrzej541
  • 919
  • 1
  • 9
  • 21
  • 1. Reconsider to not bother with reindexing an array which is indexed already. 2. Search google/SO for reindex JSON or reindex object array 3. Show a [mcve] using the `<>` snippet editor if you still have issues. PS: If you really have an array, use a standard for loop with a counter, or a foreach instead of for in. Had you posted your data we could give better suggestions – mplungjan Mar 10 '17 at 08:58
  • please add the example data and the wanted result as well. – Nina Scholz Mar 10 '17 at 09:02
  • My elements are actually quite huge json structures so it is pointless to post it here. But I have updated my post with a basic example how it look like and how I want to change it – andrzej541 Mar 10 '17 at 09:24

2 Answers2

7

If I understood you right, the problem for you is to how to insert and move elements in array. You can do it with splice method, which has optional parameters for elements to insert:

Here is what you need for your example:

var myArray = [
  {name: "not selected"}, 
  {name: "not selected"},
  {name: "selected"}, 
  {name: "not selected"},
  {name: "not selected"}, 
  {name: "not selected"}
];

myArray.splice(3, 0, {
  name: "new element"
});
console.log(myArray);
Georgy
  • 2,410
  • 3
  • 21
  • 35
  • 2
    This is assuming you know the index beforehand (which is a fair assumption). For the future visitors who don't, however, there's [`[].findIndex()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex) – Madara's Ghost Mar 10 '17 at 09:33
5

You could use Array#splice and take an index after the element gets inserted to the array.

var array = [{ name: "not selected" }, { name: "not selected" }, { name: "selected" }, { name: "not selected" }, { name: "not selected" }, { name: "not selected" }],
    index = 2,
    item = { name: "new element" };

array.splice(index + 1, 0, item);

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392