【问题标题】:how to remove inner brackets from arraylist object in javascript如何从javascript中的arraylist对象中删除内括号
【发布时间】:2018-05-20 09:43:12
【问题描述】:

我只想从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?"
}
]

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

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

【问题讨论】:

    标签: javascript object arraylist iterator


    【解决方案1】:

    这样的?

    它将您的原始数组和maps 每个元素放入一个新数组。如果原始元素是一个数组(它具有“额外的括号”),那么它不会复制该值,而是仅复制该数组的第一个元素,有效地“删除括号”。我想这就是你要找的。​​p>

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

    【讨论】:

      猜你喜欢
      • 2015-10-21
      • 1970-01-01
      • 2019-07-11
      • 2021-04-28
      • 2021-03-11
      • 2015-08-02
      相关资源
      最近更新 更多