/    Sign up×
Community /Pin to ProfileBookmark

How to splice() a multidimensional Array

I have a multidimensional array and I want to delete an item from one of the inner arrays. I have no problem using splice on 1D array but when I try with 2D I get [x][y] has no properties error..but the obj is there and I put in a var…Here is my test code..can someone please advise?

Thanks in advance!

var a = [0,1,2,3,4,5,6,7,8,9];
var b = [
[0,1,2,3],
[‘a’, ‘b’, ‘c’]
];

function show(){
alert(a);
}

function editArray(){
show();
a.splice(2, 1);
show();
}

function editArray2(){
alert(b);
b.splice([0][1], 1);
alert(b);
}

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@LogicianJun 10.2006 — I have a multidimensional array and I want to delete an item from one of the inner arrays. I have no problem using splice on 1D array but when I try with 2D I get [x][y] has no properties error..but the obj is there and I put in a var...Here is my test code..can someone please advise?

Thanks in advance!

var a = [0,1,2,3,4,5,6,7,8,9];

var b = [

[0,1,2,3],

['a', 'b', 'c']

];

function show(){

alert(a);

}

function editArray(){

show();

a.splice(2, 1);

show();

}

function editArray2(){

alert(b);

b.splice([0][1], 1);

alert(b);

}[/QUOTE]


I don't think such an approach makes much sense. If you wanted to remove element 'b' from b[1], you could use:
[CODE]b[1].splice(1,1);[/CODE]
Copy linkTweet thisAlerts:
@JatinDec 08.2021 — b[1].splice(1,1);

This is the correct way
×

Success!

Help @mivate spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...