1

I am trying to add element "delete:true" after each occurrence of "_rev " mentioned in the below sample request.

Original Request:

{
    "docs": [
        {
            "_id": "123",
            "_rev": "1-7836",
            },
        {
            "_id": "456",
            "_rev": "1-1192",
           }
         ]                         

}

Expected Request:

{
    "docs": [
        {
            "_id": "123",
            "_rev": "1-7836",
            "_deleted" :true
            },
        {
            "_id": "456",
            "_rev": "1-1192",
            "_deleted" :true
           }
         ]                         

}

When I tried the below code,the ""_deleted" :true" is getting inserted after the -rev element is closed. PFB for the same and suggest.

function main(params) {
for (var i = 0; i< params.docs.length; i++) {
for (var value in params.docs[i]) {
if(value == '_rev'  && params.docs[i]._rev ){ 
var string1 = JSON.stringify(params.docs[i]);
var str = ',';
var string2 = '"';
var string3 =str+string2+ '_deleted'+ string2+ ':' + "true" ;
var res =  string1 + string3  ;
}
}
}
}
  ######################
[
  "2018-01-23T09:44:23.568738362Z stdout:
  {\"_id\":\"123\",
  \"_rev\":\"1-7836\"},
  \"_deleted\":true"]
Diya
  • 43
  • 1
  • 9
  • Possible duplicate of [Add new attribute (element) to JSON object using JavaScript](https://stackoverflow.com/questions/736590/add-new-attribute-element-to-json-object-using-javascript) – ChristophS Jan 23 '18 at 12:21
  • What for you need the position? JSON is an **J**ava **S**cript **O**bject **N**otaion. In my opinion the position should be of no account .. – ChristophS Jan 23 '18 at 12:29

5 Answers5

0

Use map and Object.assign instead of generating a string

var output = params.docs.map( s => Object.assign( {}, {"_deleted" :true}, s ) );

You can then convert this to string using JSON.stringify( output );

Demo

var params = {
  "docs": [{
      "_id": "123",
      "_rev": "1-7836",
    },
    {
      "_id": "456",
      "_rev": "1-1192",
    }
  ]
};
var output = params.docs.map(s => Object.assign({}, {
  "_deleted": true
}, s));
console.log(output);
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

var data = {
    "docs": [
        {
            "_id": "123",
            "_rev": "1-7836",
            },
        {
            "_id": "456",
            "_rev": "1-1192",
           }
         ]                         

}
var newData = data['docs'].map(item => {
   item._delete = true
   return item
})

console.log(newData);
zabusa
  • 2,520
  • 21
  • 25
0

Why don't you simply put ._deleted attribute to doc, like this ?

function main(params) {
  for (var i = 0; i< params.docs.length; i++) {
    params.docs[i]._deleted = true;
    var res = JSON.stringify(params.docs[i]);
  }
 }
}

Or like this :

function main(params) {
  for (var i = 0; i< params.docs.length; i++) {
    params.docs[i]["_deleted"] = true;
    var res = JSON.stringify(params.docs[i]);
  }
 }
}
0

You can reference the not existing attribute directly and assign an value:

#!/usr/bin/js

var myJSON = { "docs": [ { "_id":"123", "_rev":"1-200" } ] }
console.log(myJSON);
myJSON.docs[0]["_deleted"]=true;
console.log(myJSON);

Output of example:

# js append.js 
{ docs: [ { _id: '123', _rev: '1-200' } ] }
{ docs: [ { _id: '123', _rev: '1-200', _deleted: true } ] }

Read the more extensive example here: Add new attribute (element) to JSON object using JavaScript

So this might be a duplicate ...

ChristophS
  • 598
  • 4
  • 23
  • I've read the given link again. It lacks the OPs question for the position. But what for the position is relevant inside object notation ... ? – ChristophS Jan 23 '18 at 12:27
0

This is worked for me!:

     let data = {
  docs: [
    {
      _id: "123",
      _rev: "1-7836",
    },
    {
      _id: "456",
      _rev: "1-1192",
    },
  ],
};

const array = data.docs.map((item) => {
  item._deleted = true;
  return item;
});

console.log("RESULT>>>>>", array);

Result:

{
    "docs": [
        {
            "_id": "123",
            "_rev": "1-7836",
            "_deleted" :true
            },
        {
            "_id": "456",
            "_rev": "1-1192",
            "_deleted" :true
           }
         ]                         

}

To add an element inside the 0th index of the array:

data.docs[0]._deleted = true;

To add an element by conditionally

let specificId=456

data.docs.map((item) =>
specificId === item._id?(item._deleted = true):(item._deleted =false)
amboji alur
  • 175
  • 2
  • 7