splice not overriding element in an array at the specified index inside a forEach loop
I am trying to transform an array into a slightly different format, and am using the splice for now like below:
letnewArray.splice( idx , 0 , obj );
My entire code can be seen below:
let yaml_doc_json = [{
name: 'guidIds',
ids: ['209A3935-2305-4786-960B-BB008F3E9DC9',
'C10D76D2-89A3-4F28-B64E-2D78C953B283',
'B7164432-94B3-4096-A9EA-181D0F00D25E',
'DA366048-557F-42ED-AB1D-3B2196278E86'
]
},
{
name: 'keyMessage',
ids: ['Enbril_RA_30.00',
'Enbril_RA_40.00',
'Enbril_RA_50.00',
'Enbril_RA_10.00'
]
},
{
name: 'SequenceName',
ids: ['1_KEY_LUN_09_2017_Ukr_main',
'1_KEY_LUN_09_2017_Ukr_sl01',
'1_KEY_LUN_09_2017_Ukr_sl02',
'1_KEY_LUN_09_2017_Ukr_sl03'
]
}
],
letnewArray = ;
yaml_doc_json.forEach((e, i) => {
//console.log(e);
let id_name = yaml_doc_json[i].name;
//console.log(id_name);
e['ids'].forEach((elem, idx) => {
console.log(idx);
let obj = {
id_name: elem
}
letnewArray.splice(idx, 0, obj);
});
});
console.log(letnewArray);
How come i get 12 elements in letnewArray
even though idx
is always between 0-3 ? should't the elements at the specific index be overridden everytime the forEach loop runs ? why is the overriding now happening ? what am i really doing wrong here ?
javascript
|
show 3 more comments
I am trying to transform an array into a slightly different format, and am using the splice for now like below:
letnewArray.splice( idx , 0 , obj );
My entire code can be seen below:
let yaml_doc_json = [{
name: 'guidIds',
ids: ['209A3935-2305-4786-960B-BB008F3E9DC9',
'C10D76D2-89A3-4F28-B64E-2D78C953B283',
'B7164432-94B3-4096-A9EA-181D0F00D25E',
'DA366048-557F-42ED-AB1D-3B2196278E86'
]
},
{
name: 'keyMessage',
ids: ['Enbril_RA_30.00',
'Enbril_RA_40.00',
'Enbril_RA_50.00',
'Enbril_RA_10.00'
]
},
{
name: 'SequenceName',
ids: ['1_KEY_LUN_09_2017_Ukr_main',
'1_KEY_LUN_09_2017_Ukr_sl01',
'1_KEY_LUN_09_2017_Ukr_sl02',
'1_KEY_LUN_09_2017_Ukr_sl03'
]
}
],
letnewArray = ;
yaml_doc_json.forEach((e, i) => {
//console.log(e);
let id_name = yaml_doc_json[i].name;
//console.log(id_name);
e['ids'].forEach((elem, idx) => {
console.log(idx);
let obj = {
id_name: elem
}
letnewArray.splice(idx, 0, obj);
});
});
console.log(letnewArray);
How come i get 12 elements in letnewArray
even though idx
is always between 0-3 ? should't the elements at the specific index be overridden everytime the forEach loop runs ? why is the overriding now happening ? what am i really doing wrong here ?
javascript
InspectletnewArray
after theforEach()
is done and everything should make more sense to you.
– Patrick Roberts
Nov 14 '18 at 18:22
2
The second argument ofsplice()
is thedeleteCount
. If it is zero then all you do is insert the new item and all others at that index and beyond will shift to the right
– charlietfl
Nov 14 '18 at 18:23
So what are your expected results?
– charlietfl
Nov 14 '18 at 18:25
@charlietfl my expected result would be an array of with a length of 4.
– Alexander Solonik
Nov 14 '18 at 18:47
1
Seems like all you need then is a simplemap()
of the ids in last object of main arrayletarray = yaml_doc_json[yaml_doc_json.length-1].ids.map(id=>({id_name:id}))
– charlietfl
Nov 14 '18 at 18:58
|
show 3 more comments
I am trying to transform an array into a slightly different format, and am using the splice for now like below:
letnewArray.splice( idx , 0 , obj );
My entire code can be seen below:
let yaml_doc_json = [{
name: 'guidIds',
ids: ['209A3935-2305-4786-960B-BB008F3E9DC9',
'C10D76D2-89A3-4F28-B64E-2D78C953B283',
'B7164432-94B3-4096-A9EA-181D0F00D25E',
'DA366048-557F-42ED-AB1D-3B2196278E86'
]
},
{
name: 'keyMessage',
ids: ['Enbril_RA_30.00',
'Enbril_RA_40.00',
'Enbril_RA_50.00',
'Enbril_RA_10.00'
]
},
{
name: 'SequenceName',
ids: ['1_KEY_LUN_09_2017_Ukr_main',
'1_KEY_LUN_09_2017_Ukr_sl01',
'1_KEY_LUN_09_2017_Ukr_sl02',
'1_KEY_LUN_09_2017_Ukr_sl03'
]
}
],
letnewArray = ;
yaml_doc_json.forEach((e, i) => {
//console.log(e);
let id_name = yaml_doc_json[i].name;
//console.log(id_name);
e['ids'].forEach((elem, idx) => {
console.log(idx);
let obj = {
id_name: elem
}
letnewArray.splice(idx, 0, obj);
});
});
console.log(letnewArray);
How come i get 12 elements in letnewArray
even though idx
is always between 0-3 ? should't the elements at the specific index be overridden everytime the forEach loop runs ? why is the overriding now happening ? what am i really doing wrong here ?
javascript
I am trying to transform an array into a slightly different format, and am using the splice for now like below:
letnewArray.splice( idx , 0 , obj );
My entire code can be seen below:
let yaml_doc_json = [{
name: 'guidIds',
ids: ['209A3935-2305-4786-960B-BB008F3E9DC9',
'C10D76D2-89A3-4F28-B64E-2D78C953B283',
'B7164432-94B3-4096-A9EA-181D0F00D25E',
'DA366048-557F-42ED-AB1D-3B2196278E86'
]
},
{
name: 'keyMessage',
ids: ['Enbril_RA_30.00',
'Enbril_RA_40.00',
'Enbril_RA_50.00',
'Enbril_RA_10.00'
]
},
{
name: 'SequenceName',
ids: ['1_KEY_LUN_09_2017_Ukr_main',
'1_KEY_LUN_09_2017_Ukr_sl01',
'1_KEY_LUN_09_2017_Ukr_sl02',
'1_KEY_LUN_09_2017_Ukr_sl03'
]
}
],
letnewArray = ;
yaml_doc_json.forEach((e, i) => {
//console.log(e);
let id_name = yaml_doc_json[i].name;
//console.log(id_name);
e['ids'].forEach((elem, idx) => {
console.log(idx);
let obj = {
id_name: elem
}
letnewArray.splice(idx, 0, obj);
});
});
console.log(letnewArray);
How come i get 12 elements in letnewArray
even though idx
is always between 0-3 ? should't the elements at the specific index be overridden everytime the forEach loop runs ? why is the overriding now happening ? what am i really doing wrong here ?
let yaml_doc_json = [{
name: 'guidIds',
ids: ['209A3935-2305-4786-960B-BB008F3E9DC9',
'C10D76D2-89A3-4F28-B64E-2D78C953B283',
'B7164432-94B3-4096-A9EA-181D0F00D25E',
'DA366048-557F-42ED-AB1D-3B2196278E86'
]
},
{
name: 'keyMessage',
ids: ['Enbril_RA_30.00',
'Enbril_RA_40.00',
'Enbril_RA_50.00',
'Enbril_RA_10.00'
]
},
{
name: 'SequenceName',
ids: ['1_KEY_LUN_09_2017_Ukr_main',
'1_KEY_LUN_09_2017_Ukr_sl01',
'1_KEY_LUN_09_2017_Ukr_sl02',
'1_KEY_LUN_09_2017_Ukr_sl03'
]
}
],
letnewArray = ;
yaml_doc_json.forEach((e, i) => {
//console.log(e);
let id_name = yaml_doc_json[i].name;
//console.log(id_name);
e['ids'].forEach((elem, idx) => {
console.log(idx);
let obj = {
id_name: elem
}
letnewArray.splice(idx, 0, obj);
});
});
console.log(letnewArray);
let yaml_doc_json = [{
name: 'guidIds',
ids: ['209A3935-2305-4786-960B-BB008F3E9DC9',
'C10D76D2-89A3-4F28-B64E-2D78C953B283',
'B7164432-94B3-4096-A9EA-181D0F00D25E',
'DA366048-557F-42ED-AB1D-3B2196278E86'
]
},
{
name: 'keyMessage',
ids: ['Enbril_RA_30.00',
'Enbril_RA_40.00',
'Enbril_RA_50.00',
'Enbril_RA_10.00'
]
},
{
name: 'SequenceName',
ids: ['1_KEY_LUN_09_2017_Ukr_main',
'1_KEY_LUN_09_2017_Ukr_sl01',
'1_KEY_LUN_09_2017_Ukr_sl02',
'1_KEY_LUN_09_2017_Ukr_sl03'
]
}
],
letnewArray = ;
yaml_doc_json.forEach((e, i) => {
//console.log(e);
let id_name = yaml_doc_json[i].name;
//console.log(id_name);
e['ids'].forEach((elem, idx) => {
console.log(idx);
let obj = {
id_name: elem
}
letnewArray.splice(idx, 0, obj);
});
});
console.log(letnewArray);
javascript
javascript
edited Nov 14 '18 at 18:23
Patrick Roberts
20.2k33576
20.2k33576
asked Nov 14 '18 at 18:18
Alexander SolonikAlexander Solonik
3,42863488
3,42863488
InspectletnewArray
after theforEach()
is done and everything should make more sense to you.
– Patrick Roberts
Nov 14 '18 at 18:22
2
The second argument ofsplice()
is thedeleteCount
. If it is zero then all you do is insert the new item and all others at that index and beyond will shift to the right
– charlietfl
Nov 14 '18 at 18:23
So what are your expected results?
– charlietfl
Nov 14 '18 at 18:25
@charlietfl my expected result would be an array of with a length of 4.
– Alexander Solonik
Nov 14 '18 at 18:47
1
Seems like all you need then is a simplemap()
of the ids in last object of main arrayletarray = yaml_doc_json[yaml_doc_json.length-1].ids.map(id=>({id_name:id}))
– charlietfl
Nov 14 '18 at 18:58
|
show 3 more comments
InspectletnewArray
after theforEach()
is done and everything should make more sense to you.
– Patrick Roberts
Nov 14 '18 at 18:22
2
The second argument ofsplice()
is thedeleteCount
. If it is zero then all you do is insert the new item and all others at that index and beyond will shift to the right
– charlietfl
Nov 14 '18 at 18:23
So what are your expected results?
– charlietfl
Nov 14 '18 at 18:25
@charlietfl my expected result would be an array of with a length of 4.
– Alexander Solonik
Nov 14 '18 at 18:47
1
Seems like all you need then is a simplemap()
of the ids in last object of main arrayletarray = yaml_doc_json[yaml_doc_json.length-1].ids.map(id=>({id_name:id}))
– charlietfl
Nov 14 '18 at 18:58
Inspect
letnewArray
after the forEach()
is done and everything should make more sense to you.– Patrick Roberts
Nov 14 '18 at 18:22
Inspect
letnewArray
after the forEach()
is done and everything should make more sense to you.– Patrick Roberts
Nov 14 '18 at 18:22
2
2
The second argument of
splice()
is the deleteCount
. If it is zero then all you do is insert the new item and all others at that index and beyond will shift to the right– charlietfl
Nov 14 '18 at 18:23
The second argument of
splice()
is the deleteCount
. If it is zero then all you do is insert the new item and all others at that index and beyond will shift to the right– charlietfl
Nov 14 '18 at 18:23
So what are your expected results?
– charlietfl
Nov 14 '18 at 18:25
So what are your expected results?
– charlietfl
Nov 14 '18 at 18:25
@charlietfl my expected result would be an array of with a length of 4.
– Alexander Solonik
Nov 14 '18 at 18:47
@charlietfl my expected result would be an array of with a length of 4.
– Alexander Solonik
Nov 14 '18 at 18:47
1
1
Seems like all you need then is a simple
map()
of the ids in last object of main array letarray = yaml_doc_json[yaml_doc_json.length-1].ids.map(id=>({id_name:id}))
– charlietfl
Nov 14 '18 at 18:58
Seems like all you need then is a simple
map()
of the ids in last object of main array letarray = yaml_doc_json[yaml_doc_json.length-1].ids.map(id=>({id_name:id}))
– charlietfl
Nov 14 '18 at 18:58
|
show 3 more comments
3 Answers
3
active
oldest
votes
The 12 elements in the letnewArray
come from having each of the three elements in yaml_doc_json
elaborated four times (the number of ids
elements).
The fact that there are 12 is because of the syntax of Array.prototype.splice
. The complete reference can be found here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
The important part is the syntax:
array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
Let us turn back to your code. We have
letnewArray.splice( idx , 0 , obj );
Going by the definition above, we can infer
array => letnewArray
start => idx
deleteCount => 0
item1 => obj
The various item1, item2, ...
elements are added to the array. Therefore, each time the for loop cycles through, we delete 0 items and add 1 element.
This shows why the ending elements in the letnewArray
variable are 12.
I hope to have been useful.
add a comment |
Splice will add the third argument to existing array. Refer https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_splice1
How does this answer the question?
– charlietfl
Nov 14 '18 at 18:33
I guess he is not aware of how splice works in the first place.
– Hari Prathap
Nov 14 '18 at 18:36
Certainly aware that the third argument gets inserted or wouldn't have anything in the array. The question is about the indexing
– charlietfl
Nov 14 '18 at 18:37
From his question he thinks it will override the element instead of appending.
– Hari Prathap
Nov 14 '18 at 18:39
add a comment |
@charlietfl answered this in the comments !.
The second argument of splice() is the deleteCount. If it is zero then
all you do is insert the new item and all others at that index and
beyond will shift to the right.
Then, Its not your answer is it?
– Dementic
Nov 18 '18 at 22:50
@Dementic , It is not my answer , but it is the answer that resolved my query !
– Alexander Solonik
Nov 18 '18 at 22:54
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306488%2fsplice-not-overriding-element-in-an-array-at-the-specified-index-inside-a-foreac%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The 12 elements in the letnewArray
come from having each of the three elements in yaml_doc_json
elaborated four times (the number of ids
elements).
The fact that there are 12 is because of the syntax of Array.prototype.splice
. The complete reference can be found here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
The important part is the syntax:
array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
Let us turn back to your code. We have
letnewArray.splice( idx , 0 , obj );
Going by the definition above, we can infer
array => letnewArray
start => idx
deleteCount => 0
item1 => obj
The various item1, item2, ...
elements are added to the array. Therefore, each time the for loop cycles through, we delete 0 items and add 1 element.
This shows why the ending elements in the letnewArray
variable are 12.
I hope to have been useful.
add a comment |
The 12 elements in the letnewArray
come from having each of the three elements in yaml_doc_json
elaborated four times (the number of ids
elements).
The fact that there are 12 is because of the syntax of Array.prototype.splice
. The complete reference can be found here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
The important part is the syntax:
array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
Let us turn back to your code. We have
letnewArray.splice( idx , 0 , obj );
Going by the definition above, we can infer
array => letnewArray
start => idx
deleteCount => 0
item1 => obj
The various item1, item2, ...
elements are added to the array. Therefore, each time the for loop cycles through, we delete 0 items and add 1 element.
This shows why the ending elements in the letnewArray
variable are 12.
I hope to have been useful.
add a comment |
The 12 elements in the letnewArray
come from having each of the three elements in yaml_doc_json
elaborated four times (the number of ids
elements).
The fact that there are 12 is because of the syntax of Array.prototype.splice
. The complete reference can be found here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
The important part is the syntax:
array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
Let us turn back to your code. We have
letnewArray.splice( idx , 0 , obj );
Going by the definition above, we can infer
array => letnewArray
start => idx
deleteCount => 0
item1 => obj
The various item1, item2, ...
elements are added to the array. Therefore, each time the for loop cycles through, we delete 0 items and add 1 element.
This shows why the ending elements in the letnewArray
variable are 12.
I hope to have been useful.
The 12 elements in the letnewArray
come from having each of the three elements in yaml_doc_json
elaborated four times (the number of ids
elements).
The fact that there are 12 is because of the syntax of Array.prototype.splice
. The complete reference can be found here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
The important part is the syntax:
array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
Let us turn back to your code. We have
letnewArray.splice( idx , 0 , obj );
Going by the definition above, we can infer
array => letnewArray
start => idx
deleteCount => 0
item1 => obj
The various item1, item2, ...
elements are added to the array. Therefore, each time the for loop cycles through, we delete 0 items and add 1 element.
This shows why the ending elements in the letnewArray
variable are 12.
I hope to have been useful.
answered Nov 14 '18 at 18:27
MarkioMarkio
4613
4613
add a comment |
add a comment |
Splice will add the third argument to existing array. Refer https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_splice1
How does this answer the question?
– charlietfl
Nov 14 '18 at 18:33
I guess he is not aware of how splice works in the first place.
– Hari Prathap
Nov 14 '18 at 18:36
Certainly aware that the third argument gets inserted or wouldn't have anything in the array. The question is about the indexing
– charlietfl
Nov 14 '18 at 18:37
From his question he thinks it will override the element instead of appending.
– Hari Prathap
Nov 14 '18 at 18:39
add a comment |
Splice will add the third argument to existing array. Refer https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_splice1
How does this answer the question?
– charlietfl
Nov 14 '18 at 18:33
I guess he is not aware of how splice works in the first place.
– Hari Prathap
Nov 14 '18 at 18:36
Certainly aware that the third argument gets inserted or wouldn't have anything in the array. The question is about the indexing
– charlietfl
Nov 14 '18 at 18:37
From his question he thinks it will override the element instead of appending.
– Hari Prathap
Nov 14 '18 at 18:39
add a comment |
Splice will add the third argument to existing array. Refer https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_splice1
Splice will add the third argument to existing array. Refer https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_splice1
answered Nov 14 '18 at 18:28
Hari PrathapHari Prathap
705
705
How does this answer the question?
– charlietfl
Nov 14 '18 at 18:33
I guess he is not aware of how splice works in the first place.
– Hari Prathap
Nov 14 '18 at 18:36
Certainly aware that the third argument gets inserted or wouldn't have anything in the array. The question is about the indexing
– charlietfl
Nov 14 '18 at 18:37
From his question he thinks it will override the element instead of appending.
– Hari Prathap
Nov 14 '18 at 18:39
add a comment |
How does this answer the question?
– charlietfl
Nov 14 '18 at 18:33
I guess he is not aware of how splice works in the first place.
– Hari Prathap
Nov 14 '18 at 18:36
Certainly aware that the third argument gets inserted or wouldn't have anything in the array. The question is about the indexing
– charlietfl
Nov 14 '18 at 18:37
From his question he thinks it will override the element instead of appending.
– Hari Prathap
Nov 14 '18 at 18:39
How does this answer the question?
– charlietfl
Nov 14 '18 at 18:33
How does this answer the question?
– charlietfl
Nov 14 '18 at 18:33
I guess he is not aware of how splice works in the first place.
– Hari Prathap
Nov 14 '18 at 18:36
I guess he is not aware of how splice works in the first place.
– Hari Prathap
Nov 14 '18 at 18:36
Certainly aware that the third argument gets inserted or wouldn't have anything in the array. The question is about the indexing
– charlietfl
Nov 14 '18 at 18:37
Certainly aware that the third argument gets inserted or wouldn't have anything in the array. The question is about the indexing
– charlietfl
Nov 14 '18 at 18:37
From his question he thinks it will override the element instead of appending.
– Hari Prathap
Nov 14 '18 at 18:39
From his question he thinks it will override the element instead of appending.
– Hari Prathap
Nov 14 '18 at 18:39
add a comment |
@charlietfl answered this in the comments !.
The second argument of splice() is the deleteCount. If it is zero then
all you do is insert the new item and all others at that index and
beyond will shift to the right.
Then, Its not your answer is it?
– Dementic
Nov 18 '18 at 22:50
@Dementic , It is not my answer , but it is the answer that resolved my query !
– Alexander Solonik
Nov 18 '18 at 22:54
add a comment |
@charlietfl answered this in the comments !.
The second argument of splice() is the deleteCount. If it is zero then
all you do is insert the new item and all others at that index and
beyond will shift to the right.
Then, Its not your answer is it?
– Dementic
Nov 18 '18 at 22:50
@Dementic , It is not my answer , but it is the answer that resolved my query !
– Alexander Solonik
Nov 18 '18 at 22:54
add a comment |
@charlietfl answered this in the comments !.
The second argument of splice() is the deleteCount. If it is zero then
all you do is insert the new item and all others at that index and
beyond will shift to the right.
@charlietfl answered this in the comments !.
The second argument of splice() is the deleteCount. If it is zero then
all you do is insert the new item and all others at that index and
beyond will shift to the right.
answered Nov 18 '18 at 22:36
Alexander SolonikAlexander Solonik
3,42863488
3,42863488
Then, Its not your answer is it?
– Dementic
Nov 18 '18 at 22:50
@Dementic , It is not my answer , but it is the answer that resolved my query !
– Alexander Solonik
Nov 18 '18 at 22:54
add a comment |
Then, Its not your answer is it?
– Dementic
Nov 18 '18 at 22:50
@Dementic , It is not my answer , but it is the answer that resolved my query !
– Alexander Solonik
Nov 18 '18 at 22:54
Then, Its not your answer is it?
– Dementic
Nov 18 '18 at 22:50
Then, Its not your answer is it?
– Dementic
Nov 18 '18 at 22:50
@Dementic , It is not my answer , but it is the answer that resolved my query !
– Alexander Solonik
Nov 18 '18 at 22:54
@Dementic , It is not my answer , but it is the answer that resolved my query !
– Alexander Solonik
Nov 18 '18 at 22:54
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306488%2fsplice-not-overriding-element-in-an-array-at-the-specified-index-inside-a-foreac%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Inspect
letnewArray
after theforEach()
is done and everything should make more sense to you.– Patrick Roberts
Nov 14 '18 at 18:22
2
The second argument of
splice()
is thedeleteCount
. If it is zero then all you do is insert the new item and all others at that index and beyond will shift to the right– charlietfl
Nov 14 '18 at 18:23
So what are your expected results?
– charlietfl
Nov 14 '18 at 18:25
@charlietfl my expected result would be an array of with a length of 4.
– Alexander Solonik
Nov 14 '18 at 18:47
1
Seems like all you need then is a simple
map()
of the ids in last object of main arrayletarray = yaml_doc_json[yaml_doc_json.length-1].ids.map(id=>({id_name:id}))
– charlietfl
Nov 14 '18 at 18:58