从嵌套在对象内的数组中解析值。

时间:2017-08-23 07:12:56

标签: javascript

给出以下对象:

const object = {
    greeting: "hi",
    farewell:"bye",
    specialArray:[10,20,30,40,50]
} 

我需要将数组的3个第一个元素检索到3个单独的变量abc

如何?

4 个答案:

答案 0 :(得分:1)

只需使用destructuring assignment的变量分配给数组。

const object = {
        greeting: "hi",
        farewell: "bye",
        specialArray: [10, 20, 30, 40, 50]
    },
    [a, b, c] = object.specialArray;

console.log(a, b, c);

答案 1 :(得分:0)

您可以将其分配给示例中给出的变量



const object = {
    greeting: "hi",
    farewell:"bye",
    specialArray:[10,20,30,40,50]
} 

let [a, b, c] = object.specialArray;

console.log(a, b, c);




答案 2 :(得分:0)

我不确定这是你期望的答案,但你提出的问题似乎很简单:

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)

答案 3 :(得分:0)

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
相关问题