How to remove key+value pair from an object of Array

This question already has an answer here:

  • How do I remove a property from a JavaScript object? 33 answers

  • 你可以使用forEach

     let result=[
        {
           'id':'1',
           'b':'asd'
        },
        {
           'id':'2',
           'b':'asd'
        },
        { 
           'id':'2000',
           'b':'asd'
        },
     ];
    result.forEach(function(item){ delete item.b });
    console.log(result);

    if that one is the only structure you have you can do this

    result = result.map(e => ({ id: e.id }))
    

    or if the structure is far more complicated, you might want to use delete :

    result.forEach((e) => {
        delete e.b;
    });
    

    尝试下面的代码:

    let result=[
        {
           'id':'1',
           'b':'asd'
        },
        {
           'id':'2',
           'b':'asd'
        },
        { 
           'id':'2000',
           'b':'asd'
        }
     ];
     
     for(let i in result){
        let obj = result[i];
        delete obj['b'];
     }
     console.log(result);
    链接地址: http://www.djcxy.com/p/4684.html

    上一篇: Object.prototype是Verboten?

    下一篇: 如何从Array对象中删除键+值对