Tal vez sea una pregunta extraña, y tal vez eso exista y otra forma de hacer eso ... Pero, tengo un Objeto inicial en el que itero en la clave. Para una tarea especial, necesitaba tener este Objeto sin alguna clave: valor. Para hacer eso hago un objeto con la llave que no quiero.
Hago este lápiz de código para muestra: https://codepen.io/charlene-bx/pen/qBXaqEL
const myInitialObject = { 'key#one' : 'dataofanotherObject', 'key#two' : 'dataofanotherObject', 'key#three' : 'dataofanotherObject', 'key#four' : 'dataofanotherObject', 'key#five' : 'dataofanotherObject', 'key#six' : 'dataofanotherObject', 'key#seven' : 'dataofanotherObject', 'key#eight' : 'dataofanotherObject', 'key#nine' : 'dataofanotherObject', 'key#ten' : 'dataofanotherObject' } const unDesiredKey = [ 'one', 'four', 'six' ] // expected output: // { // 'key#two' : 'dataofanotherObject', // 'key#three' : 'dataofanotherObject', // 'key#five' : 'dataofanotherObject', // 'key#seven' : 'dataofanotherObject', // 'key#eight' : 'dataofanotherObject', // 'key#nine' : 'dataofanotherObject', // 'key#ten' : 'dataofanotherObject' // }Encontré esta solución pero no puedo encontrarla muy legible:
const filteredObject = Object.keys(myInitialObject) .filter(key => !unDesiredKey.map( el => !key.includes(el)).some(el => el===false)) .reduce((accumulator, value) => ({...accumulator, [value]: myInitialObject[value]}), {})Podrías hacer esto,
Use Array.prototype.map() en Object.entires() para filtrar una matriz del par clave-valor deseado. Y finalmente construya un nuevo objeto a partir del par clave-valor usando Object.fromEntries()
const myInitialObject = { 'key#one': 'dataofanotherObject', 'key#two': 'dataofanotherObject', 'key#three': 'dataofanotherObject', 'key#four': 'dataofanotherObject', 'key#five': 'dataofanotherObject', 'key#six': 'dataofanotherObject', 'key#seven': 'dataofanotherObject', 'key#eight': 'dataofanotherObject', 'key#nine': 'dataofanotherObject', 'key#ten': 'dataofanotherObject' } const unDesiredKey = [ 'one', 'four', 'six' ] const arr = Object.entries(myInitialObject).filter(x => !unDesiredKey.includes(x[0].split('#')[1])); const finalObj = Object.fromEntries(arr); console.log(finalObj)Puede filter las entries de los objetos manteniendo las que evalúan falsy para find(x => k.includes(x)) y usarlas para crear un nuevo objeto fromEntries .
const myInitialObject = { 'key#one': 'dataofanotherObject', 'key#two': 'dataofanotherObject', 'key#three': 'dataofanotherObject', 'key#four': 'dataofanotherObject', 'key#five': 'dataofanotherObject', 'key#six': 'dataofanotherObject', 'key#seven': 'dataofanotherObject', 'key#eight': 'dataofanotherObject', 'key#nine': 'dataofanotherObject', 'key#ten': 'dataofanotherObject' } const unDesiredKey = [ 'one', 'four', 'six' ]; const result = Object.fromEntries(Object.entries(myInitialObject) .filter(([k]) => !unDesiredKey.find(x => k.includes(x))) ); console.log(result); También podría lograr esto con reduce() si lo desea.
const obj = { 'key#one': 'dataofanotherObject', 'key#two': 'dataofanotherObject', 'key#three': 'dataofanotherObject', 'key#four': 'dataofanotherObject', 'key#five': 'dataofanotherObject', 'key#six': 'dataofanotherObject', 'key#seven': 'dataofanotherObject', 'key#eight': 'dataofanotherObject', 'key#nine': 'dataofanotherObject', 'key#ten': 'dataofanotherObject' } const unDesiredKey = [ 'one', 'four', 'six' ]; const result = Object.entries(obj).reduce((acc, [k, v]) => { if (unDesiredKey.find(x => k.includes(x))) return acc; acc = { ...acc, [k]: v } return acc; }, {}); console.log(result);Puede filtrar las entradas y dividir la clave para comparar.
const data = { 'key#one': 'dataofanotherObject', 'key#two': 'dataofanotherObject', 'key#three': 'dataofanotherObject', 'key#four': 'dataofanotherObject', 'key#five': 'dataofanotherObject', 'key#six': 'dataofanotherObject', 'key#seven': 'dataofanotherObject', 'key#eight': 'dataofanotherObject', 'key#nine': 'dataofanotherObject', 'key#ten': 'dataofanotherObject' }, unDesiredKey = ['one', 'four', 'six'], result = Object.fromEntries(Object .entries(data) .filter(([key]) => !unDesiredKey.includes(key.slice(key.indexOf('#') + 1))) ); console.log(result); .as-console-wrapper { max-height: 100% !important; top: 0; }