Keeping only certain properties in a JavaScript object

Asked
Active3 hr before
Viewed126 times

8 Answers

javascriptcertainkeepingobjectproperties
90%

I want to keep only "b1" properties of all objects.,Thanks for contributing an answer to Stack Overflow!,Another way is to delete certain properties, which is less effective:, Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers

Just re-initialise the object:

myObj = {
   p1: myObj.p1,
   p2: myObj.p2,
   p100: myObj.p100
};

Another way is to delete certain properties, which is less effective:

var prop = ['p1', 'p2', 'p100'];
for (var k in myObj) {
   if (prop.indexOf(k) < 0) {
      delete myObj[k];
   }
}
load more v
88%

An array of strings that corresponds to the properties found directly in the given object.,The Object.getOwnPropertyNames() method returns an array of all properties (including non-enumerable properties except for those which use Symbol) found directly in a given object.,Items on the prototype chain are not listed:,The object whose enumerable and non-enumerable properties are to be returned.

Object.getOwnPropertyNames(obj)
load more v
72%

const object = {
   a: 5,
   b: 6,
   c: 7
};
const picked = (({
   a,
   c
}) => ({
   a,
   c
}))(object);

console.log(picked); // { a: 5, c: 7 }
load more v
65%

When a for loop looks like this, with the word of after a variable definition, it will loop over the elements of the value given after of. This works not only for arrays but also for strings and some other data structures. We’ll discuss how it works in Chapter 6.,There’s an Object.assign function that copies all properties from one object into another.,The introduction of this book alluded to the following as a nice way to compute the sum of a range of numbers:,A string can be repeated with the repeat method, which creates a new string containing multiple copies of the original string, glued together.

To run over a list (in listToArray and nth), a for loop specification like this can be used:

for (let node = list; node; node = node.rest) {}
75%

Summary: in this tutorial, you will learn how to check if a property exists in an object.,JavaScript provides you with three common ways to check if a property exists in an object:,Use the in operator to check if a property exists in both own properties and inherited properties of an object.,Use the hasOwnProperty() method to check if an property exists in the own properties of an object.

The JavaScript Object.prototype has the method hasOwnProperty() that returns true if a property exists in an object:

.wp - block - code {
      border: 0;
      padding: 0;
   }

   .wp - block - code > div {
      overflow: auto;
   }

   .shcb - language {
      border: 0;
      clip: rect(1 px, 1 px, 1 px, 1 px); -
      webkit - clip - path: inset(50 % );
      clip - path: inset(50 % );
      height: 1 px;
      margin: -1 px;
      overflow: hidden;
      padding: 0;
      position: absolute;
      width: 1 px;
      word - wrap: normal;
      word - break: normal;
   }

   .hljs {
      box - sizing: border - box;
   }

   .hljs.shcb - code - table {
      display: table;
      width: 100 % ;
   }

   .hljs.shcb - code - table > .shcb - loc {
      color: inherit;
      display: table - row;
      width: 100 % ;
   }

   .hljs.shcb - code - table.shcb - loc > span {
      display: table - cell;
   }

   .wp - block - code code.hljs: not(.shcb - wrap - lines) {
      white - space: pre;
   }

   .wp - block - code code.hljs.shcb - wrap - lines {
      white - space: pre - wrap;
   }

   .hljs.shcb - line - numbers {
      border - spacing: 0;
      counter - reset: line;
   }

   .hljs.shcb - line - numbers > .shcb - loc {
      counter - increment: line;
   }

   .hljs.shcb - line - numbers.shcb - loc > span {
      padding - left: 0.75 em;
   }

   .hljs.shcb - line - numbers.shcb - loc::before {
      border - right: 1 px solid #ddd;
      content: counter(line);
      display: table - cell;
      padding: 0 0.75 em;
      text - align: right; -
      webkit - user - select: none; -
      moz - user - select: none; -
      ms - user - select: none;
      user - select: none;
      white - space: nowrap;
      width: 1 % ;
   }
let result = targetObject.hasOwnProperty(propertyName);
Code language: JavaScript(javascript)
load more v
40%

Literals and properties,We can immediately put some properties into {...} as “key: value” pairs:,Property value shorthand,To access a property, we can use:

let user = new Object(); // "object constructor" syntax
let user = {}; // "object literal" syntax
load more v
22%

First we need an example object to loop over. So I put some of my experience in it 😉 (hahaha)! Keep the fun in it!,The last example is the For-in loop to loop over the properties of the Object it.,The 3 methods to loop over Object Properties in JavaScript are:,In this post I want to show you how you can easily loop over Object properties with 3 different methods.

First we need an example object to loop over. So I put some of my experience in it 😉 (hahaha)! Keep the fun in it!

let experienceObject = {
   name: 'Raymon',
   title: 'Lead Frontend/JavaScript Developer',
   yearsExperience: 8,
}
load more v
60%

Arrays are converted into observable arrays. If an update would change the number of items, it will perform the appropriate add/remove actions. It will also try to keep the order the same as the original JavaScript array.,data: The JavaScript object containing the data for this child,The options argument supplied to your create callback is a JavaScript object containing:,All properties of an object are converted into an observable. If an update would change the value, it will update the observable.

var viewModel = {
   serverTime: ko.observable(),
   numUsers: ko.observable()
}
load more v

Other "javascript-certain" queries related to "Keeping only certain properties in a JavaScript object"