1

Given the code below, how can I have item name "C" appear in the 3rd position correctly?

const foo = [
  {
    name: 'a',
  },
  {
    name: 'b'
  }
]

const bar = [
  {
    name: 'c',
    position: [1, 3]
  },
  {
    name: 'd',
    position: [2]
  }
]

bar.forEach((item) => {
  item.position.forEach((pos) => {
      foo.splice(pos - 1, 0, item);
  });
})

console.log(foo);

Output

[
  { name: 'c', position: [ 1, 3 ] },
  { name: 'd', position: [ 2 ] },
  { name: 'a' },
  { name: 'c', position: [ 1, 3 ] },
  { name: 'b' }
]

Desired output

[
  { name: 'c', position: [ 1, 3 ] },
  { name: 'd', position: [ 2 ] },
  { name: 'c', position: [ 1, 3 ] },
  { name: 'a' },
  { name: 'b' }
]
2
  • is position unique? Commented Jan 17, 2023 at 16:29
  • 1
    @NinaScholz yes, it will be Commented Jan 17, 2023 at 16:36

2 Answers 2

1

You can sort all of the objects to insert by their index before adding them sequentially.

const foo = [
  {
    name: 'a',
  },
  {
    name: 'b'
  }
];
const bar = [
  {
    name: 'c',
    position: [1, 3]
  },
  {
    name: 'd',
    position: [2]
  }
]

bar.flatMap(x => x.position.map(p => [p, x]))
  .sort(([p1], [p2]) => p1 - p2).forEach(([p, o]) => foo.splice(p - 1, 0, o));
console.log(JSON.stringify(foo));

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

Comments

0

You could build a temporary array with the wanted indices and splice with order.

const
    foo = [{ name: 'a' }, { name: 'b' }],
    bar = [{ name: 'c', position: [1, 3] }, { name: 'd', position: [2] }];

bar
    .reduce((r, item) => {
        item.position.forEach(pos => r[pos - 1] = item);
        return r;
    }, [])
    .forEach((item, i) => foo.splice(i, 0, item));

console.log(foo);
.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.