简体   繁体   中英

How to access JSON array elements from Input to output which are similar in structure

I have to transfrom input json to output json message using javascript Below are input and output test samples Input

[{
   "Id": "123",
   "Address": "12",
   "Cadre": "LOLOL" 
   "Meeting": {"Output":    [
            {
         "Date": "2020-08-13T15:00:00Z",
         "comment": "Working"
      },
            {
         "timestamp": "2020-08-13T10:03:01Z",
         "comment": "Learning"
      },
          
     ]}
        
    },
    {
   "Id": "345",
   "Address": "14",
   "Cadre": "Loop" 
   "Meeting": {"Output":    [
            {
         "Date": "2020-12-12T18:27:00Z",
         "comment": "Working"
      },
            {
         "timestamp": "2020-11-22T14:53:01Z",
         "comment": "Learning"
      },
          
     ]}
        
    }   
]

Output also should remain same, with out changes to other fields except the array in "Meeting/Output/comment" field.

Output

[{
   "Id": "123",
   "Address": "12",
   "Cadre": "LOLOL" 
   "Meeting": {"Output":    [
            {
         "Date": "2020-08-13T15:00:00Z",
         "comment": "NOt known"
      },
            {
         "timestamp": "2020-08-13T10:03:01Z",
         "comment": "Taken"
      },
          
     ]}
        
    },
    {
   "Id": "345",
   "Address": "14",
   "Cadre": "Loop" 
   "Meeting": {"Output":    [
            {
         "Date": "2020-12-12T18:27:00Z",
         **"comment": "Should Not be"**
      },
            {
         "timestamp": "2020-11-22T14:53:01Z",
         **"comment": "Help///A"**
      },
          
     ]}
        
    }   
]

I have taken "for" loop to iterate on Input payload array object and retrieved the "Meeting" object made necessary changes I have tried this

session.input.readAsJSON(function(readAsJSONError, payload) {   
    var incomingDataLength = payload.length;    
    for(var i=0; i<incomingDataLength; i++){        
        var reqArr = payload.Meeting.Output;
        if(reqArr === null || reqArr === '' || reqArr === undefined) {
        console.log("Do something");
        } else {
        var reqArrLength = payload.Meeting.Output.length;
        for(var i=0;  i<reqArrLength;  i++){
            reqArrLength[i].comment= callsomefunction(reqArrLength[i].comment);
        }
        }       
    }

});

Could you please suggest me on this to proceed.

The simplest way to do it is by using Array.map

var output = input;
output.Meeting.Output = input.Meeting.Output.map(item =>
{
  delete item.comment;
  return item;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM