Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

delete object not working

// define dis-allowed keys and values
const disAllowedKeys = ['_id','__v','password'];
const disAllowedValues = [null, undefined, ''];

// our object, maybe a Mongoose model, or some API response
const someObject = {
  _id: 132456789,
  password: '$1$O3JMY.Tw$AdLnLjQ/5jXF9.MTp3gHv/',
  name: 'John Edward',
  age: 29,
  favoriteFood: null
}; 

// use reduce to create a new object with everything EXCEPT our dis-allowed keys and values!
const withOnlyGoodValues = Object.entries(someObject).reduce((ourNewObject, pair) => {
  const key = pair[0];
  const value = pair[1]; 
  if (
    disAllowedKeys.includes(key) === false &&
    disAllowedValues.includes(value) === false
  ){
    ourNewObject[key] = value; 
  }
  return ourNewObject; 
}, {}); 

// what we get back...
// {
//   name: 'John Edward',
//   age: 29
// }

// do something with the new object!
server.sendToClient(withOnlyGoodValues);
Comment

PREVIOUS NEXT
Code Example
Javascript :: rewrite expressjs url 
Javascript :: pass parameter to javascript function onclick 
Javascript :: javascript example 
Javascript :: create a component in react 
Javascript :: jquery select 
Javascript :: how to add eventlister to multiple variable 
Javascript :: node js command line interface 
Javascript :: string to svg react 
Javascript :: check cookies client side 
Javascript :: js alert with multiple buttons 
Javascript :: delete js 
Javascript :: Find index using arrow function 
Javascript :: splice remove 0 elements before index and insert new element 
Javascript :: one signal api to send notification 
Javascript :: react-redux todo list 
Javascript :: javascript substration between times 
Javascript :: link external file by url using javascript 
Javascript :: javascript get the last item in an array 
Javascript :: javascript sorting an array 
Javascript :: Origin http://localhost:3002 is not allowed by Access-Control-Allow-Origin. 
Javascript :: flightphp 
Javascript :: how to use fetch api 
Javascript :: if( request()-ajax()==false 
Javascript :: react form validation 
Javascript :: carousel in material ui react 
Javascript :: javascript repeat function 
Javascript :: proxy api javascript set 
Javascript :: shape of array in js 
Javascript :: javascript Recursionexample 
Javascript :: why is this undefined in react 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =