What is the proper way to get an element from an array with array destructuring? [duplicate]












-2















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);









share|improve this question















marked as duplicate by briosheje, Jared Smith, Praveen Kumar Purushothaman javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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
















-2















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);









share|improve this question















marked as duplicate by briosheje, Jared Smith, Praveen Kumar Purushothaman javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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














-2












-2








-2








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);









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago

























asked 2 days ago









Crayzzit

427




427




marked as duplicate by briosheje, Jared Smith, Praveen Kumar Purushothaman javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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 javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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














  • 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












1 Answer
1






active

oldest

votes


















1














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






share|improve this answer





















  • 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


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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






share|improve this answer





















  • 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
















1














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






share|improve this answer





















  • 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














1












1








1






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






share|improve this answer












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







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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



Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'