Skip to content

updateObject(object,newValues,query)

Bhumil Sarvaiya edited this page Aug 31, 2017 · 1 revision

example

//common data
var daex = require('./index')
var obj = [
  {
    name: 'abc',
    type: 'array',
    age: 20
  },
  {
    name: 'pqr',
    type: 'string',
    age: 20
  },
  {
    name: 'xyz',
    type: 'array',
    age: 22
  },
  {
    name: 'ghi',
    type: 'array',
    age: 20
  },
  {
    name: 'mno',
    type: 'number',
    age: 20
  }
]
//e.g. 1
daex.updateObject(obj[1],{'name':'PQR','age':12})
console.log(obj)

//Output
//[ { name: 'abc', type: 'array', age: 20 },
//  { name: 'PQR', type: 'string', age: 12 },
//  { name: 'xyz', type: 'array', age: 22 },
//  { name: 'ghi', type: 'array', age: 20 },
//  { name: 'mno', type: 'number', age: 20 } ]
//e.g. 2
daex.updateObject(obj[1],{'name':'PQR','age':12},{'name':'abc'})
console.log(obj)

//Output
//[ { name: 'abc', type: 'array', age: 20 },
//  { name: 'pqr', type: 'string', age: 20 },
//  { name: 'xyz', type: 'array', age: 22 },
//  { name: 'ghi', type: 'array', age: 20 },
//  { name: 'mno', type: 'number', age: 20 } ]

//i.e. no update as obj[1] does not satisfy the query: {'name':'abc'}
//e.g. 3
daex.updateObject(obj[0],{'name':'PQR','age':12},{'name':'abc'})
console.log(obj)

//Output
//[ { name: 'PQR', type: 'array', age: 12 },
//  { name: 'pqr', type: 'string', age: 20 },
//  { name: 'xyz', type: 'array', age: 22 },
//  { name: 'ghi', type: 'array', age: 20 },
//  { name: 'mno', type: 'number', age: 20 } ]

//i.e. obj[0] got updated as it satisfy the query: {'name':'abc'}