How to splice an array out of a nested array - javascript
I am attempting to splice a nested array out of its parent array. Consider the following array. items.splice(0,1)
should give me the first nested array([1,2]), however it seems to give me the first nested array, still nested inside of an array:
var items = [[1,2],[3,4],[5,6]];
var item = items.splice(0,1); // should slice first array out of items array
console.log(item); //should log [1,2], instead logs [[1,2]]
However it seems to return the needed array (first item), in another array. And I'm not able to get the full array unless I do item[0]
. What in the world am I missing!?
javascript arrays multidimensional-array splice
add a comment |
I am attempting to splice a nested array out of its parent array. Consider the following array. items.splice(0,1)
should give me the first nested array([1,2]), however it seems to give me the first nested array, still nested inside of an array:
var items = [[1,2],[3,4],[5,6]];
var item = items.splice(0,1); // should slice first array out of items array
console.log(item); //should log [1,2], instead logs [[1,2]]
However it seems to return the needed array (first item), in another array. And I'm not able to get the full array unless I do item[0]
. What in the world am I missing!?
javascript arrays multidimensional-array splice
1
No need for splice. Just useitems[0]
instead.
– PHPglue
Sep 1 '15 at 2:22
Well I want to alter the original array and then chose a value based on an element of the spliced array.
– Afs35mm
Sep 1 '15 at 19:24
add a comment |
I am attempting to splice a nested array out of its parent array. Consider the following array. items.splice(0,1)
should give me the first nested array([1,2]), however it seems to give me the first nested array, still nested inside of an array:
var items = [[1,2],[3,4],[5,6]];
var item = items.splice(0,1); // should slice first array out of items array
console.log(item); //should log [1,2], instead logs [[1,2]]
However it seems to return the needed array (first item), in another array. And I'm not able to get the full array unless I do item[0]
. What in the world am I missing!?
javascript arrays multidimensional-array splice
I am attempting to splice a nested array out of its parent array. Consider the following array. items.splice(0,1)
should give me the first nested array([1,2]), however it seems to give me the first nested array, still nested inside of an array:
var items = [[1,2],[3,4],[5,6]];
var item = items.splice(0,1); // should slice first array out of items array
console.log(item); //should log [1,2], instead logs [[1,2]]
However it seems to return the needed array (first item), in another array. And I'm not able to get the full array unless I do item[0]
. What in the world am I missing!?
javascript arrays multidimensional-array splice
javascript arrays multidimensional-array splice
edited Sep 1 '15 at 2:55
user663031
asked Sep 1 '15 at 2:17
Afs35mmAfs35mm
101211
101211
1
No need for splice. Just useitems[0]
instead.
– PHPglue
Sep 1 '15 at 2:22
Well I want to alter the original array and then chose a value based on an element of the spliced array.
– Afs35mm
Sep 1 '15 at 19:24
add a comment |
1
No need for splice. Just useitems[0]
instead.
– PHPglue
Sep 1 '15 at 2:22
Well I want to alter the original array and then chose a value based on an element of the spliced array.
– Afs35mm
Sep 1 '15 at 19:24
1
1
No need for splice. Just use
items[0]
instead.– PHPglue
Sep 1 '15 at 2:22
No need for splice. Just use
items[0]
instead.– PHPglue
Sep 1 '15 at 2:22
Well I want to alter the original array and then chose a value based on an element of the spliced array.
– Afs35mm
Sep 1 '15 at 19:24
Well I want to alter the original array and then chose a value based on an element of the spliced array.
– Afs35mm
Sep 1 '15 at 19:24
add a comment |
5 Answers
5
active
oldest
votes
The MDN says Array.prototype.splice
:
Returns
An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.
So, it won't return just the deleted element, it will be wrapped in an array.
Kind of crazy I never knew that splice automatically places it inside a newly created array, I kind of just always assumed it worked that way because it was being taken from an already existing array. Thinking about it that makes sense because that's how Array.prototype.slice.call(arguments) works considering it's only an array-like object :)
– Afs35mm
Sep 1 '15 at 19:25
add a comment |
splice()
changes the contents of an array, and returns an array with the deleted elements.
In your case, your original array has elements that also happen to be arrays. That might be confusing you.
add a comment |
.splice()
is returning correct array at item
; try selecting index 0
of returned array to return [1,2]
; to "flatten" item
, try utilizing Array.prototype.concat()
; see How to flatten array in jQuery?
var items = [[1,2],[3,4],[5,6]];
var item = items.splice(0,1); // should slice first array out of items array
// https://stackoverflow.com/a/7875193/
var arr = .concat.apply(, item);
console.log(item[0], arr);
add a comment |
You should reference the first array in items, and then splice it. Try working snippet below.
var items = [[1,2],[3,4],[5,6]];
var item = items[0].splice(0,2);
console.log(item);
add a comment |
To replace a two dimensional array you should use array[row][col] for example
for(var row = 0; row < numbers.length; row++) {
for(var col = 0; col < numbers[row].length; col++) {
if (numbers[row][col] % 2 === 0) {
numbers[row][col] = "even";
} else {
numbers[row][col] = "odd";
}
}
}
console.log(numbers);
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%2f32322544%2fhow-to-splice-an-array-out-of-a-nested-array-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
The MDN says Array.prototype.splice
:
Returns
An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.
So, it won't return just the deleted element, it will be wrapped in an array.
Kind of crazy I never knew that splice automatically places it inside a newly created array, I kind of just always assumed it worked that way because it was being taken from an already existing array. Thinking about it that makes sense because that's how Array.prototype.slice.call(arguments) works considering it's only an array-like object :)
– Afs35mm
Sep 1 '15 at 19:25
add a comment |
The MDN says Array.prototype.splice
:
Returns
An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.
So, it won't return just the deleted element, it will be wrapped in an array.
Kind of crazy I never knew that splice automatically places it inside a newly created array, I kind of just always assumed it worked that way because it was being taken from an already existing array. Thinking about it that makes sense because that's how Array.prototype.slice.call(arguments) works considering it's only an array-like object :)
– Afs35mm
Sep 1 '15 at 19:25
add a comment |
The MDN says Array.prototype.splice
:
Returns
An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.
So, it won't return just the deleted element, it will be wrapped in an array.
The MDN says Array.prototype.splice
:
Returns
An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.
So, it won't return just the deleted element, it will be wrapped in an array.
answered Sep 1 '15 at 2:28
MinusFourMinusFour
8,74121828
8,74121828
Kind of crazy I never knew that splice automatically places it inside a newly created array, I kind of just always assumed it worked that way because it was being taken from an already existing array. Thinking about it that makes sense because that's how Array.prototype.slice.call(arguments) works considering it's only an array-like object :)
– Afs35mm
Sep 1 '15 at 19:25
add a comment |
Kind of crazy I never knew that splice automatically places it inside a newly created array, I kind of just always assumed it worked that way because it was being taken from an already existing array. Thinking about it that makes sense because that's how Array.prototype.slice.call(arguments) works considering it's only an array-like object :)
– Afs35mm
Sep 1 '15 at 19:25
Kind of crazy I never knew that splice automatically places it inside a newly created array, I kind of just always assumed it worked that way because it was being taken from an already existing array. Thinking about it that makes sense because that's how Array.prototype.slice.call(arguments) works considering it's only an array-like object :)
– Afs35mm
Sep 1 '15 at 19:25
Kind of crazy I never knew that splice automatically places it inside a newly created array, I kind of just always assumed it worked that way because it was being taken from an already existing array. Thinking about it that makes sense because that's how Array.prototype.slice.call(arguments) works considering it's only an array-like object :)
– Afs35mm
Sep 1 '15 at 19:25
add a comment |
splice()
changes the contents of an array, and returns an array with the deleted elements.
In your case, your original array has elements that also happen to be arrays. That might be confusing you.
add a comment |
splice()
changes the contents of an array, and returns an array with the deleted elements.
In your case, your original array has elements that also happen to be arrays. That might be confusing you.
add a comment |
splice()
changes the contents of an array, and returns an array with the deleted elements.
In your case, your original array has elements that also happen to be arrays. That might be confusing you.
splice()
changes the contents of an array, and returns an array with the deleted elements.
In your case, your original array has elements that also happen to be arrays. That might be confusing you.
answered Sep 1 '15 at 2:24
cybersamcybersam
39.5k43151
39.5k43151
add a comment |
add a comment |
.splice()
is returning correct array at item
; try selecting index 0
of returned array to return [1,2]
; to "flatten" item
, try utilizing Array.prototype.concat()
; see How to flatten array in jQuery?
var items = [[1,2],[3,4],[5,6]];
var item = items.splice(0,1); // should slice first array out of items array
// https://stackoverflow.com/a/7875193/
var arr = .concat.apply(, item);
console.log(item[0], arr);
add a comment |
.splice()
is returning correct array at item
; try selecting index 0
of returned array to return [1,2]
; to "flatten" item
, try utilizing Array.prototype.concat()
; see How to flatten array in jQuery?
var items = [[1,2],[3,4],[5,6]];
var item = items.splice(0,1); // should slice first array out of items array
// https://stackoverflow.com/a/7875193/
var arr = .concat.apply(, item);
console.log(item[0], arr);
add a comment |
.splice()
is returning correct array at item
; try selecting index 0
of returned array to return [1,2]
; to "flatten" item
, try utilizing Array.prototype.concat()
; see How to flatten array in jQuery?
var items = [[1,2],[3,4],[5,6]];
var item = items.splice(0,1); // should slice first array out of items array
// https://stackoverflow.com/a/7875193/
var arr = .concat.apply(, item);
console.log(item[0], arr);
.splice()
is returning correct array at item
; try selecting index 0
of returned array to return [1,2]
; to "flatten" item
, try utilizing Array.prototype.concat()
; see How to flatten array in jQuery?
var items = [[1,2],[3,4],[5,6]];
var item = items.splice(0,1); // should slice first array out of items array
// https://stackoverflow.com/a/7875193/
var arr = .concat.apply(, item);
console.log(item[0], arr);
var items = [[1,2],[3,4],[5,6]];
var item = items.splice(0,1); // should slice first array out of items array
// https://stackoverflow.com/a/7875193/
var arr = .concat.apply(, item);
console.log(item[0], arr);
var items = [[1,2],[3,4],[5,6]];
var item = items.splice(0,1); // should slice first array out of items array
// https://stackoverflow.com/a/7875193/
var arr = .concat.apply(, item);
console.log(item[0], arr);
edited May 23 '17 at 12:34
Community♦
11
11
answered Sep 1 '15 at 2:36
guest271314guest271314
79.2k643114
79.2k643114
add a comment |
add a comment |
You should reference the first array in items, and then splice it. Try working snippet below.
var items = [[1,2],[3,4],[5,6]];
var item = items[0].splice(0,2);
console.log(item);
add a comment |
You should reference the first array in items, and then splice it. Try working snippet below.
var items = [[1,2],[3,4],[5,6]];
var item = items[0].splice(0,2);
console.log(item);
add a comment |
You should reference the first array in items, and then splice it. Try working snippet below.
var items = [[1,2],[3,4],[5,6]];
var item = items[0].splice(0,2);
console.log(item);
You should reference the first array in items, and then splice it. Try working snippet below.
var items = [[1,2],[3,4],[5,6]];
var item = items[0].splice(0,2);
console.log(item);
var items = [[1,2],[3,4],[5,6]];
var item = items[0].splice(0,2);
console.log(item);
var items = [[1,2],[3,4],[5,6]];
var item = items[0].splice(0,2);
console.log(item);
edited Jan 20 at 0:14
answered Jan 20 at 0:01
M'BoulasM'Boulas
45212
45212
add a comment |
add a comment |
To replace a two dimensional array you should use array[row][col] for example
for(var row = 0; row < numbers.length; row++) {
for(var col = 0; col < numbers[row].length; col++) {
if (numbers[row][col] % 2 === 0) {
numbers[row][col] = "even";
} else {
numbers[row][col] = "odd";
}
}
}
console.log(numbers);
add a comment |
To replace a two dimensional array you should use array[row][col] for example
for(var row = 0; row < numbers.length; row++) {
for(var col = 0; col < numbers[row].length; col++) {
if (numbers[row][col] % 2 === 0) {
numbers[row][col] = "even";
} else {
numbers[row][col] = "odd";
}
}
}
console.log(numbers);
add a comment |
To replace a two dimensional array you should use array[row][col] for example
for(var row = 0; row < numbers.length; row++) {
for(var col = 0; col < numbers[row].length; col++) {
if (numbers[row][col] % 2 === 0) {
numbers[row][col] = "even";
} else {
numbers[row][col] = "odd";
}
}
}
console.log(numbers);
To replace a two dimensional array you should use array[row][col] for example
for(var row = 0; row < numbers.length; row++) {
for(var col = 0; col < numbers[row].length; col++) {
if (numbers[row][col] % 2 === 0) {
numbers[row][col] = "even";
} else {
numbers[row][col] = "odd";
}
}
}
console.log(numbers);
answered Oct 20 '17 at 21:25
Alicia GuzmanAlicia Guzman
2414
2414
add a comment |
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%2f32322544%2fhow-to-splice-an-array-out-of-a-nested-array-javascript%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
1
No need for splice. Just use
items[0]
instead.– PHPglue
Sep 1 '15 at 2:22
Well I want to alter the original array and then chose a value based on an element of the spliced array.
– Afs35mm
Sep 1 '15 at 19:24