-3

For example,

var a = {prop1:'asd',prop2:'zxc'};
console.log(a);

and the intended return is just one of the property and not all shown, like

{prop1:'asd'} // or just asd

Selective property are being displayed only, without going through

a.prop1

but just

a
BeyondAnt
  • 1
  • 3

2 Answers2

0

You can simply delete the property from the object by using

delete a.prop2;
// or
delete a['prop2'];
console.log(a);
// output will be {prop1: 'asd'}

If you are using ES6 you can use built-in Reflect object to delete object property by calling Reflect.deleteProperty() function with target object and property key as parameters:

Reflect.deleteProperty(a, 'prop2');
mshameer
  • 3,431
  • 2
  • 14
  • 15
0

From what I can gather... You could create a function that takes an object and a key as 2 arguments, then loop through the object and find the key.

function findKey(object, key) {
    for(prop in object) {
      if (prop == key) return object[prop];
    }

    return null;
}

console.log(findKey({prop1: 'foo', prop2: 'bar'}, 'prop1'));

// Output would be: foo
Joshua Russell
  • 670
  • 5
  • 13