0

I want to build a function that will always insert JSON Items in an array at the last item before.

const array = [ 
  {India: { Score: '12', PlayedMatched: '21' } },
  { China: { Score: '52', PlayedMatched: '51' }},
  { USA: { Score: '15', PlayedMatched: '06' } },
  // Insert Items Here
  { 'Index Value': 12 }      
]

const insert = (arr, index, newItem) => [
    ...arr.slice(0, index),
    newItem,
    ...arr.slice(index)
]


var insertItems= [{ 'Japan': { Score: "54", PlayedMatched: "56" } },{ 'Russia': { Score: "99", PlayedMatched: "178" } }];

// I have tried like this: array.slice(-1) to insert item one step before always

const result = insert(array,array.slice(-1),insertItems)

console.log(result);

OutPut:


[ [ { Japan: [Object] }, { Japan: [Object] } ],     //Not at Here
  { India: { Score: '12', PlayedMatched: '21' } },
  { China: { Score: '52', PlayedMatched: '51' } },
  { USA: { Score: '15', PlayedMatched: '06' } },
  { 'Index Value': 12 } ]

Expected Output:

[ 
  { India: { Score: '12', PlayedMatched: '21' } },
  { China: { Score: '52', PlayedMatched: '51' } },
  { USA: { Score: '15', PlayedMatched: '06' } },
  [ { Japan: [Object] }, { Japan: [Object] } ],      //At here
  { 'Index Value': 12 }
]

How should i do this? And I also want to remove the this: [] in my Output Results. :)

Like This:

 [ 
  {India: { Score: '12', PlayedMatched: '21' } },
  { China: { Score: '52', PlayedMatched: '51' }},
  { USA: { Score: '15', PlayedMatched: '06' } },
  { 'Japan': { Score: "54", PlayedMatched: "56" } },
  { 'Russia': { Score: "99", PlayedMatched: "178" } },
  { 'Index Value': 12 }      
]
Ivar
  • 6,138
  • 12
  • 49
  • 61
Divyanshu Sah
  • 218
  • 4
  • 13
  • Unrelated to your problem, but `{India: { Score: '12', PlayedMatched: '21' } },` doesn't seem like a very usable object structure to me. `{team: "India", score: 12, playedMatches: 21}` makes more sense to me, so you needn't do `Object.keys` or something just to figure out what team it is. Or consider an outer object keyed by team name rather than an array. – ggorlen Jul 11 '21 at 15:37
  • 1
    There is nothing related to JSON in your question. – Ivar Jul 11 '21 at 15:38
  • 1
    Does this answer your question? [How to insert an item into an array at a specific index (JavaScript)?](https://stackoverflow.com/questions/586182/how-to-insert-an-item-into-an-array-at-a-specific-index-javascript) – ggorlen Jul 11 '21 at 15:39

2 Answers2

0

Use the splice function like so:

const array = [ 
  {India: { Score: '12', PlayedMatched: '21' } },
  { China: { Score: '52', PlayedMatched: '51' }},
  { USA: { Score: '15', PlayedMatched: '06' } },
  { 'Index Value': 12 }      
]


var insertItems= [{ 'Japan': { Score: "54", PlayedMatched: "56" } },{ 'Russia': { Score: "99", PlayedMatched: "178" } }];



array.splice(array.length - 1, 0, ...insertItems)

console.log(array);

Notice that splice modify the same array you give it and doesn't create a modified copy of it.

Pineapples
  • 56
  • 3
  • I was going to post the same thing with a snippet, but I'll refrain. Pls consider rounding out your answer with OP's code and a testable snippet. – Kinglish Jul 11 '21 at 15:38
0

Just pass the index number where you want to insert as index parameter in insert function to get the job done. In your case last second place so, you can pass array.length - 1

const array = [ 
  {India: { Score: '12', PlayedMatched: '21' } },
  { China: { Score: '52', PlayedMatched: '51' }},
  { USA: { Score: '15', PlayedMatched: '06' } },
  { 'Index Value': 12 }      
]

const insert = (arr, index, newItem) => [
    ...arr.slice(0, index),
    ...newItem,
    ...arr.slice(index)
]


var insertItems= [{ 'Japan': { Score: "54", PlayedMatched: "56" } },{ 'Russia': { Score: "99", PlayedMatched: "178" } }];



const result = insert(array,array.length - 1,insertItems)

console.log(result);
Bin Rohan
  • 370
  • 4
  • 13