89310

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

<div class="question-status question-originals-of-duplicate">

This question already has an answer here:

    <li> How do I remove a property from a JavaScript object? <span class="question-originals-answer-count"> 35 answers </span> </li> </ul>

    I want to remove property 'b' from all objects how i remove???

    let result=[ { 'id':'1', 'b':'asd' }, { 'id':'2', 'b':'asd' }, ... , { 'id':'2000', 'b':'asd' }, ] // delete object.b; its only for object and want to remove from whole // array 2000 records

    Using foreach loop is it correct way to delete key + value pair from array of objects

    Answer1:

    You could use forEach.

    <div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

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

    Answer2:

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

    Answer3:

    Try following code:

    <div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

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

    Hope it helps :)

Recommend

  • AngularJS sorting a list with nulls
  • Regexp, capture between parentheses, javascript
  • SQL: Getting the physical size of a subset of a table
  • Extract All Possible Paths from Expression-Tree and evaluate them to hold TRUE
  • XSLT foreach repeating nodes to flat
  • How do I remove all but some records based on a threshold?
  • How do I get the list of bad records that didn't load in Bigquery?
  • Undefined references when compiling gSOAP client
  • List images(01.png) and descriptions(01.txt) from directory
  • Web.config system.webserver errors
  • Sequential (transactional) API calls in angular 4 with state management
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • Mysterious problem with floating point in LISP - time axis generation
  • How to know when stdin is empty if it contains EOF?
  • D3 nodes and links from JSON with nested arrays of children
  • Adding a button at the bottom of a table view
  • Deselecting radio buttons while keeping the View Model in synch
  • Regex thinks I'm nesting, but I'm not
  • Getting last autonumber in access
  • What is the “return” in scheme?
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Delete MySQLi record without showing the id in the URL
  • Rearranging Cells in UITableView Bug & Saving Changes
  • GridView Sorting works once only
  • 0x202A in filename: Why?
  • Numpy divide by zero. Why?
  • php design question - will a Helper help here?
  • How to disable jQuery.jplayer autoplay?
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • AngularJs get employee from factory
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • How to stop GridView from loading again when I press back button?
  • File not found error Google Drive API
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Reading document lines to the user (python)