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"},
]
3
  • 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 minimal reproducible example 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 Commented Mar 10, 2017 at 8:58
  • please add the example data and the wanted result as well. Commented Mar 10, 2017 at 9: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 Commented Mar 10, 2017 at 9:24

2 Answers 2

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);

Sign up to request clarification or add additional context in comments.

1 Comment

This is assuming you know the index beforehand (which is a fair assumption). For the future visitors who don't, however, there's [].findIndex()
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; }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.