Chủ đề này mô tả cách loại trừ các thuộc tính công khai trên các đối tượng được lập chỉ mục trong Tìm kiếm & Điều hướng được Tối ưu hóa
Đề xuất chỉnh sửa
Làm thế nào nó hoạt động Theo mặc định, tất cả các thuộc tính công khai trên một đối tượng được bao gồm khi lập chỉ mục cho nó. Cách đơn giản nhất để loại trừ một thuộc tính là chú thích thuộc tính JsonIgnore trong Newtonsoft. không gian tên json Ví dụ C#using Newtonsoft.Json;
public class User
{
public string Username { get; set; }
[JsonIgnore]
public string Password { get; set; }
}
Bạn cũng có thể loại trừ các thuộc tính hoặc các trường khác đã bao gồm trước đó bằng cách tùy chỉnh các quy ước Máy khách
C#
//using EPiServer.Find.ClientConventions;
client.Conventions
.ForInstancesOf[]
.ExcludeField[x => x.Password];
Đoạn mã trên loại trừ thuộc tính Mật khẩu khỏi các thể hiện của lớp Người dùng cũng như các thể hiện của các lớp kế thừa lớp Người dùng
Bạn có một đối tượng với một số thuộc tính và bạn muốn loại bỏ một số thuộc tính này trước khi sử dụng thêm đối tượng
let person = {
firstName: "John",
lastName: "Doe",
gender: "Male",
age: 34
};
const json = JSON.stringify[person];
console.log[json];
// => {"firstName":"John","lastName":"Doe","gender":"Male","age":34}
// What can we do if we don't want the `age` property in the JSON string? -->
Ở đây, chuỗi JSON cũng chứa thuộc tính
//using EPiServer.Find.ClientConventions;
client.Conventions
.ForInstancesOf[]
.ExcludeField[x => x.Password];
9. Tuy nhiên, bạn sẽ gửi chuỗi qua mạng và máy chủ ở đầu bên kia sẽ không sử dụng thuộc tính //using EPiServer.Find.ClientConventions;
client.Conventions
.ForInstancesOf[]
.ExcludeField[x => x.Password];
9. Vì vậy, bạn muốn xóa thuộc tính //using EPiServer.Find.ClientConventions;
client.Conventions
.ForInstancesOf[]
.ExcludeField[x => x.Password];
9 khỏi đối tượng let person = {
firstName: "John",
lastName: "Doe",
gender: "Male",
age: 34
};
const json = JSON.stringify[person];
console.log[json];
// => {"firstName":"John","lastName":"Doe","gender":"Male","age":34}
// What can we do if we don't want the `age` property in the JSON string? -->
2 trước khi chuyển đổi nó thành chuỗi JSONGiải pháp
Bạn có thể sử dụng toán tử
let person = {
firstName: "John",
lastName: "Doe",
gender: "Male",
age: 34
};
const json = JSON.stringify[person];
console.log[json];
// => {"firstName":"John","lastName":"Doe","gender":"Male","age":34}
// What can we do if we don't want the `age` property in the JSON string? -->
3, đơn giản hơn hoặc hủy đối tượng, có thể xóa nhiều thuộc tính cùng một lúcSử dụng toán tử let person = {
firstName: "John",
lastName: "Doe",
gender: "Male",
age: 34
};
const json = JSON.stringify[person];
console.log[json];
// => {"firstName":"John","lastName":"Doe","gender":"Male","age":34}
// What can we do if we don't want the `age` property in the JSON string? -->
3
let person = {
firstName: "John",
lastName: "Doe",
gender: "Male",
age: 34
};
const json = JSON.stringify[person];
console.log[json];
// => {"firstName":"John","lastName":"Doe","gender":"Male","age":34}
// What can we do if we don't want the `age` property in the JSON string? -->
Sử dụng toán tử
let person = {
firstName: "John",
lastName: "Doe",
gender: "Male",
age: 34
};
const json = JSON.stringify[person];
console.log[json];
// => {"firstName":"John","lastName":"Doe","gender":"Male","age":34}
// What can we do if we don't want the `age` property in the JSON string? -->
3 để xóa thuộc tính khỏi đối tượng let person = {
firstName: "John",
lastName: "Doe",
gender: "Male",
age: 34
};
// Delete the age property first
delete person.age;
let json = JSON.stringify[person];
console.log[json];
Toán tử
let person = {
firstName: "John",
lastName: "Doe",
gender: "Male",
age: 34
};
const json = JSON.stringify[person];
console.log[json];
// => {"firstName":"John","lastName":"Doe","gender":"Male","age":34}
// What can we do if we don't want the `age` property in the JSON string? -->
3 sẽ trả về nếu thành công và luôn trả về let person = {
firstName: "John",
lastName: "Doe",
gender: "Male",
age: 34
};
// Delete the age property first
delete person.age;
let json = JSON.stringify[person];
console.log[json];
1 - ngay cả khi thuộc tính không tồn tại. Cuộc gọi sẽ chỉ trả về let person = {
firstName: "John",
lastName: "Doe",
gender: "Male",
age: 34
};
// Delete the age property first
delete person.age;
let json = JSON.stringify[person];
console.log[json];
2 khi một thuộc tính không thể định cấu hình - đó là trường hợp đối với các thuộc tính trên các đối tượng tích hợp như let person = {
firstName: "John",
lastName: "Doe",
gender: "Male",
age: 34
};
// Delete the age property first
delete person.age;
let json = JSON.stringify[person];
console.log[json];
3 của một let person = {
firstName: "John",
lastName: "Doe",
gender: "Male",
age: 34
};
// Delete the age property first
delete person.age;
let json = JSON.stringify[person];
console.log[json];
4Sử dụng phá hủy đối tượng
Toán tử
let person = {
firstName: "John",
lastName: "Doe",
gender: "Male",
age: 34
};
const json = JSON.stringify[person];
console.log[json];
// => {"firstName":"John","lastName":"Doe","gender":"Male","age":34}
// What can we do if we don't want the `age` property in the JSON string? -->
3 chỉ có thể xóa một thuộc tính cho mỗi cuộc gọi. Vì vậy, nếu bạn muốn xóa các thuộc tính //using EPiServer.Find.ClientConventions;
client.Conventions
.ForInstancesOf[]
.ExcludeField[x => x.Password];
9 và let person = {
firstName: "John",
lastName: "Doe",
gender: "Male",
age: 34
};
// Delete the age property first
delete person.age;
let json = JSON.stringify[person];
console.log[json];
7, thì bạn phải thực hiện hai cuộc gọi let person = {
firstName: "John",
lastName: "Doe",
gender: "Male",
age: 34
};
const json = JSON.stringify[person];
console.log[json];
// => {"firstName":"John","lastName":"Doe","gender":"Male","age":34}
// What can we do if we don't want the `age` property in the JSON string? -->
3. Thay vào đó, bạn có thể sử dụng tính năng hủy đối tượng để xóa nhiều thuộc tính bằng một lệnh gọi const person = {
firstName: "John",
lastName: "Doe",
gender: "Male",
age: 34
};
// Destructure the age and gender
const {age, gender, ...personTrimmed} = person;
const json = JSON.stringify[personTrimmed];
console.log[json];
Lưu ý rằng quá trình hủy chậm hơn đáng kể so với toán tử
let person = {
firstName: "John",
lastName: "Doe",
gender: "Male",
age: 34
};
const json = JSON.stringify[person];
console.log[json];
// => {"firstName":"John","lastName":"Doe","gender":"Male","age":34}
// What can we do if we don't want the `age` property in the JSON string? -->
3 vì nó tạo một đối tượng mới sao chép từ đối tượng ban đầu. Hủy cấu trúc cũng không được hỗ trợ bởi bất kỳ phiên bản IE nào Trong bài đăng này, bạn sẽ tìm hiểu cách xóa các thuộc tính khỏi một đối tượng trong JavaScript bằng cách sử dụng cấu trúc hủy và cú pháp
let person = {
firstName: "John",
lastName: "Doe",
gender: "Male",
age: 34
};
// Delete the age property first
delete person.age;
let json = JSON.stringify[person];
console.log[json];
1Cách mới này để loại bỏ các thuộc tính của đối tượng [bằng cách loại trừ chúng khỏi một đối tượng mới] mang lại cho chúng ta một cách viết JavaScript rõ ràng hơn, với ít mã hơn và dễ đọc hơn - cũng như đây là một hoạt động không thể thay đổi
Trước khi phá hủy, chúng tôi thường sử dụng từ khóa
let person = {
firstName: "John",
lastName: "Doe",
gender: "Male",
age: 34
};
const json = JSON.stringify[person];
console.log[json];
// => {"firstName":"John","lastName":"Doe","gender":"Male","age":34}
// What can we do if we don't want the `age` property in the JSON string? -->
3 để xóa các thuộc tính khỏi một đối tượng. Vấn đề với let person = {
firstName: "John",
lastName: "Doe",
gender: "Male",
age: 34
};
const json = JSON.stringify[person];
console.log[json];
// => {"firstName":"John","lastName":"Doe","gender":"Male","age":34}
// What can we do if we don't want the `age` property in the JSON string? -->
3 là nó là một hoạt động có thể thay đổi, thay đổi vật lý đối tượng và có khả năng gây ra các tác dụng phụ không mong muốn do cách JavaScript xử lý các tham chiếu đối tượngBằng cách sử dụng tính năng hủy đối tượng, kết hợp với toán tử còn lại
let person = {
firstName: "John",
lastName: "Doe",
gender: "Male",
age: 34
};
// Delete the age property first
delete person.age;
let json = JSON.stringify[person];
console.log[json];
1, chúng ta có một cách tiếp cận đơn giảnPhá hủy đối tượng là gì?
Việc hủy đối tượng cho phép chúng ta tạo các biến từ tên thuộc tính đối tượng và biến sẽ chứa giá trị của tên thuộc tính - ví dụ
//using EPiServer.Find.ClientConventions;
client.Conventions
.ForInstancesOf[]
.ExcludeField[x => x.Password];
0Bằng cách sử dụng
let person = {
firstName: "John",
lastName: "Doe",
gender: "Male",
age: 34
};
// Delete the age property first
delete person.age;
let json = JSON.stringify[person];
console.log[json];
5, chúng tôi đang khai báo 3 biến mới [//using EPiServer.Find.ClientConventions;
client.Conventions
.ForInstancesOf[]
.ExcludeField[x => x.Password];
10, //using EPiServer.Find.ClientConventions;
client.Conventions
.ForInstancesOf[]
.ExcludeField[x => x.Password];
11 và //using EPiServer.Find.ClientConventions;
client.Conventions
.ForInstancesOf[]
.ExcludeField[x => x.Password];
12]Nếu
//using EPiServer.Find.ClientConventions;
client.Conventions
.ForInstancesOf[]
.ExcludeField[x => x.Password];
10, //using EPiServer.Find.ClientConventions;
client.Conventions
.ForInstancesOf[]
.ExcludeField[x => x.Password];
11 và //using EPiServer.Find.ClientConventions;
client.Conventions
.ForInstancesOf[]
.ExcludeField[x => x.Password];
12 tồn tại dưới dạng tên thuộc tính trên //using EPiServer.Find.ClientConventions;
client.Conventions
.ForInstancesOf[]
.ExcludeField[x => x.Password];
16, thì các biến sẽ được tạo có chứa các giá trị của thuộc tính đối tượng. Nếu tên thuộc tính không tồn tại, bạn sẽ nhận được //using EPiServer.Find.ClientConventions;
client.Conventions
.ForInstancesOf[]
.ExcludeField[x => x.Password];
17…nghỉ ngơi trong quá trình phá hủy đối tượng
Đầu tiên là các tham số còn lại, sau đó là các thuộc tính còn lại
Đã thêm vào ECMAScript 2015 và hiện ở Giai đoạn 4 - chúng ở đây để duy trì và sử dụng cho đến ngày hôm nay
Với suy nghĩ này, làm thế nào để
let person = {
firstName: "John",
lastName: "Doe",
gender: "Male",
age: 34
};
// Delete the age property first
delete person.age;
let json = JSON.stringify[person];
console.log[json];
1 giúp chúng ta loại bỏ một thuộc tính khỏi một đối tượng thông qua phá hủy?Sách điện tử miễn phí
Chỉ thị, đơn giản phải không? . Nhìn bề ngoài chúng có vẻ đơn giản, nhưng ngay cả các nhà phát triển Angular lành nghề cũng chưa nắm hết mọi khái niệm trong eBook này