如何在javascript中从arraylist对象中删除内部括号

时间:2017-12-06 13:27:05

标签: javascript object arraylist iterator

我只想从arrayList下面删除一个额外的括号:

[
{
"small":"https://www.googleapis.com/photo.png?",
"medium":"https://www.googleapis.com/photo.png?",
"big":"https://www.googleapis.com/photo.png?"
},
[
{
"small":"https://www.googleapis.com/Jenner.jfif?",
"medium":"https://www.googleapis.com/Jenner.jfif?",
"big":"https://www.googleapis.com/Jenner.jfif?"
}
]
]

to 

[
{
"small":"https://www.googleapis.com/photo.png?",
"medium":"https://www.googleapis.com/photo.png?",
"big":"https://www.googleapis.com/photo.png?"
},
{
"small":"https://www.googleapis.com/Jenner.jfif?",
"medium":"https://www.googleapis.com/Jenner.jfif?",
"big":"https://www.googleapis.com/Jenner.jfif?"
}
]

请帮我解决这个问题.....谢谢

...............................................

1 个答案:

答案 0 :(得分:2)

这样的东西?

将原始数组和maps每个元素放到一个新数组中。如果原始元素是一个数组(它有'额外的括号'),那么它不是复制值,而是复制该数组的第一个元素,有效地“删除括号”。我认为这就是你要找的东西。

var a = [
    {
        "small": "https://www.googleapis.com/photo.png?",
        "medium": "https://www.googleapis.com/photo.png?",
        "big": "https://www.googleapis.com/photo.png?"
    },
    [
        {
            "small": "https://www.googleapis.com/Jenner.jfif?",
            "medium": "https://www.googleapis.com/Jenner.jfif?",
            "big": "https://www.googleapis.com/Jenner.jfif?"
        }
    ]
];

a = a.map(elem => {
    if (elem.constructor === Array) {
        return elem[0];
    } else {
        return elem;
    }
});

console.log(a);