Reputation: 2480
Given the following object:
const object = {
greeting: "hi",
farewell:"bye",
specialArray:[10,20,30,40,50]
}
I need to retrieve the 3 first elements of the array into 3 separate variables a
, b
, c
how?
Upvotes: 1
Views: 71
Reputation: 968
const object = {
greeting: "hi",
farewell:"bye",
specialArray:[10,20,30,40,50]
};
let [a,b,c, ...rest] = object.specialArray;
console.log(a); // 10
console.log(...rest); // 40 50
Upvotes: 0
Reputation: 364
I'm not sure that is the answer you expect but what you are asking seems quite simple :
const object = {
greeting: "hi",
farewell:"bye",
specialArray:[10,20,30,40,50]
}
var [a, b, c] = object.specialArray;
console.log('a : ' + a +', b : ' + b + ', c : '+ c)
Upvotes: 0
Reputation: 10458
You can assign it to variables like given in the example
const object = {
greeting: "hi",
farewell:"bye",
specialArray:[10,20,30,40,50]
}
let [a, b, c] = object.specialArray;
console.log(a, b, c);
Upvotes: 0
Reputation: 386550
Just assign to an array with the variables for a destructuring assignment.
const object = {
greeting: "hi",
farewell: "bye",
specialArray: [10, 20, 30, 40, 50]
},
[a, b, c] = object.specialArray;
console.log(a, b, c);
Upvotes: 1