What is the proper way to get an element from an array with array destructuring? [duplicate]
This question already has an answer here:
Array destructuring in JavaScript
5 answers
Pretty much everything is in the title but i'll give you an example :
// i can't know the content of this string but for the exemple she's here
let str = 'elem-0, elem-1, elem-2, elem-3';
// how to properly write this line because this line is not write in an array 'destructuring way'
let array = str.split(',')[0];
// return me : "elem-0" (and he has to)
console.log(array);
javascript arrays destructuring
marked as duplicate by briosheje, Jared Smith, Praveen Kumar Purushothaman
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
2 days ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Array destructuring in JavaScript
5 answers
Pretty much everything is in the title but i'll give you an example :
// i can't know the content of this string but for the exemple she's here
let str = 'elem-0, elem-1, elem-2, elem-3';
// how to properly write this line because this line is not write in an array 'destructuring way'
let array = str.split(',')[0];
// return me : "elem-0" (and he has to)
console.log(array);
javascript arrays destructuring
marked as duplicate by briosheje, Jared Smith, Praveen Kumar Purushothaman
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
2 days ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
let [array] = str.split(',');
– Jared Smith
2 days ago
Depends on what you want to achieve
– Just code
2 days ago
add a comment |
This question already has an answer here:
Array destructuring in JavaScript
5 answers
Pretty much everything is in the title but i'll give you an example :
// i can't know the content of this string but for the exemple she's here
let str = 'elem-0, elem-1, elem-2, elem-3';
// how to properly write this line because this line is not write in an array 'destructuring way'
let array = str.split(',')[0];
// return me : "elem-0" (and he has to)
console.log(array);
javascript arrays destructuring
This question already has an answer here:
Array destructuring in JavaScript
5 answers
Pretty much everything is in the title but i'll give you an example :
// i can't know the content of this string but for the exemple she's here
let str = 'elem-0, elem-1, elem-2, elem-3';
// how to properly write this line because this line is not write in an array 'destructuring way'
let array = str.split(',')[0];
// return me : "elem-0" (and he has to)
console.log(array);
This question already has an answer here:
Array destructuring in JavaScript
5 answers
javascript arrays destructuring
javascript arrays destructuring
edited 2 days ago
asked 2 days ago
Crayzzit
427
427
marked as duplicate by briosheje, Jared Smith, Praveen Kumar Purushothaman
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
2 days ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by briosheje, Jared Smith, Praveen Kumar Purushothaman
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
2 days ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
let [array] = str.split(',');
– Jared Smith
2 days ago
Depends on what you want to achieve
– Just code
2 days ago
add a comment |
1
let [array] = str.split(',');
– Jared Smith
2 days ago
Depends on what you want to achieve
– Just code
2 days ago
1
1
let [array] = str.split(',');– Jared Smith
2 days ago
let [array] = str.split(',');– Jared Smith
2 days ago
Depends on what you want to achieve
– Just code
2 days ago
Depends on what you want to achieve
– Just code
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
You can destructure array elements like
let [first, second, ...rest] = str.split(',');
Basically it works by index, the first variable being arr[0], second being arr1 and so on. ...rest will hold the remaining items in the array which aren't destrucutred
Check the MDN documentation for Array Destructuring
You should avoid answering questions that have duplicates that answer the questions in detail (Based on your profile, I see you're doing this frequently). You should at least take the time to select and link the duplicates. If the questions are answered again and again, there will be thousands of short and badly answered questions and the well answered questions will be lost.
– t.niese
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can destructure array elements like
let [first, second, ...rest] = str.split(',');
Basically it works by index, the first variable being arr[0], second being arr1 and so on. ...rest will hold the remaining items in the array which aren't destrucutred
Check the MDN documentation for Array Destructuring
You should avoid answering questions that have duplicates that answer the questions in detail (Based on your profile, I see you're doing this frequently). You should at least take the time to select and link the duplicates. If the questions are answered again and again, there will be thousands of short and badly answered questions and the well answered questions will be lost.
– t.niese
2 days ago
add a comment |
You can destructure array elements like
let [first, second, ...rest] = str.split(',');
Basically it works by index, the first variable being arr[0], second being arr1 and so on. ...rest will hold the remaining items in the array which aren't destrucutred
Check the MDN documentation for Array Destructuring
You should avoid answering questions that have duplicates that answer the questions in detail (Based on your profile, I see you're doing this frequently). You should at least take the time to select and link the duplicates. If the questions are answered again and again, there will be thousands of short and badly answered questions and the well answered questions will be lost.
– t.niese
2 days ago
add a comment |
You can destructure array elements like
let [first, second, ...rest] = str.split(',');
Basically it works by index, the first variable being arr[0], second being arr1 and so on. ...rest will hold the remaining items in the array which aren't destrucutred
Check the MDN documentation for Array Destructuring
You can destructure array elements like
let [first, second, ...rest] = str.split(',');
Basically it works by index, the first variable being arr[0], second being arr1 and so on. ...rest will hold the remaining items in the array which aren't destrucutred
Check the MDN documentation for Array Destructuring
answered 2 days ago
Shubham Khatri
78.4k1492127
78.4k1492127
You should avoid answering questions that have duplicates that answer the questions in detail (Based on your profile, I see you're doing this frequently). You should at least take the time to select and link the duplicates. If the questions are answered again and again, there will be thousands of short and badly answered questions and the well answered questions will be lost.
– t.niese
2 days ago
add a comment |
You should avoid answering questions that have duplicates that answer the questions in detail (Based on your profile, I see you're doing this frequently). You should at least take the time to select and link the duplicates. If the questions are answered again and again, there will be thousands of short and badly answered questions and the well answered questions will be lost.
– t.niese
2 days ago
You should avoid answering questions that have duplicates that answer the questions in detail (Based on your profile, I see you're doing this frequently). You should at least take the time to select and link the duplicates. If the questions are answered again and again, there will be thousands of short and badly answered questions and the well answered questions will be lost.
– t.niese
2 days ago
You should avoid answering questions that have duplicates that answer the questions in detail (Based on your profile, I see you're doing this frequently). You should at least take the time to select and link the duplicates. If the questions are answered again and again, there will be thousands of short and badly answered questions and the well answered questions will be lost.
– t.niese
2 days ago
add a comment |
1
let [array] = str.split(',');– Jared Smith
2 days ago
Depends on what you want to achieve
– Just code
2 days ago