3

I have been trying to solve this program. I have a list of objects that look like this. Adding elements to an array based on their index position.

let A = {index: 0} 
let B = {index: 0} 
let C = { index: 2} 
let D = {index: 2} 
let E = { index: 1}

So if A get pushed inside the array it takes over an array index position 0. However, when B get pushed in the array, it will take over an index position. [B, A], and so on. It is sort of like first go in, first come out, except get shifted to the left. However, I want to do something like this. [B, A, C], I want to add D to index position of C. [B, A, D, C]. A is at index position 1. I want to insert E to index 1. [B, E, A, D, C]

2
  • where do you add the elements? please add the code as well. Commented Apr 11, 2018 at 18:38
  • You can probably use Array.concat(). Also please include the code you currently have when posting a question. Commented Apr 11, 2018 at 18:38

3 Answers 3

3
  function insert(array, el) {
     let pos = 0;
     while(array[pos].index < el.index) pos++;
     array.splice(pos, 0, el);
  }

Just do insertion sort and use splice to add the element.

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

4 Comments

@nina oh really? I think its really clear what the OP wants :)
Why do we need the while loop if we already know the position to insert at. Like doesn't OP mean to insert D at pos = 2 or did I misunderstand?
@hollyleaves actually that depends on how you interpret the question.
You're right, I see you actually tried to preserve the index order
2

You could simply splice the array for adding an object at the wanted index.

var a = { index: 0, value: 'a' },
    b = { index: 0, value: 'b' },
    c = { index: 2, value: 'c' },
    d = { index: 2, value: 'd' },
    e = { index: 1, value: 'e' },
    array = [];

function add(array, object) {
    array.splice(object.index, 0, object);
}

add(array, a);
add(array, b);
add(array, c);
add(array, d);
add(array, e);

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

3 Comments

Oh right, removing thanks in advance from the question made it on-topic?! Thats interesting ...
No, but you complained about my answer, and now surprisingly you add an answer eventhough the question hasnt really changed...
right, after reading you answer and checking the wanted outcoume, i thought a concise answer would help.
0

You can use the function splice and check for the index attribute to insert at a specific position.

If the index attribute doesn't exist, push the object at the end.

var decorate = (arr) => {
  arr.insertByIndex = (...objs) => {
    objs.forEach(obj => {
      if (!isNaN(obj.index)) arr.splice(obj.index, 0, obj);
      else arr.push(obj);
    });
  }
  
  return arr;
};

let A = {index: 0, desc: 'A'};
let B = {index: 0, desc: 'B'};
let C = {index: 2, desc: 'C'};
let D = {index: 2, desc: 'D'};
let E = {index: 1, desc: 'E'};

var array = decorate([]);
array.insertByIndex(D, A, C, B, E);

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.