我得到了这个输入

    var input=[ "Axel",
                4,
                4.21,
                { name : 'Bob', age : 16 },
                { type : 'fish', model : 'golden fish' },
                [1,2,3],
                "John",
                { name : 'Peter', height: 1.90}          ];


结果必须是这个

    [ { name : 'Bob', age : 16 },
      { type : 'fish', model : 'golden fish' },
      { name : 'Peter', height: 1.90}            ];

最佳答案

使用Array.prototype.filter,仅保留不是数组的对象

var input = ["Axel",
    4,
    4.21,
    {name: 'Bob', age: 16},
    {type: 'fish', model: 'golden fish'},
    [1, 2, 3],
    "John",
    {name: 'Peter', height: 1.90}
];

input = input.filter(function (e) {
    return (typeof e === 'object') && !Array.isArray(e);
}); /*
[
    {"name": "Bob", "age": 16},
    {"type": "fish", "model": "golden fish"},
    {"name": "Peter", "height": 1.9}
]
*/

关于javascript - 如何从JavaScript中的对象中删除“整数”数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29304311/

10-11 22:16
查看更多