简体   繁体   English

如何保存 object 的初始语句?

[英]How to save initial statement of object?

I have object of type:我有 object 类型:

export interface IGroupLanguage {
  name: string;
  languages?: Language[];
}

let data = [ { "name": "Automation", "languages": [ { "Name": "English", "Lcid": 1, "RightToLeft": true, "Code": "EN", "Mapped": true } ] }, { "name": "Monitors", "languages": [ { "Name": "Russian", "Lcid": 2, "RightToLeft": true, "Code": "RU", "Mapped": true } ] } ];

Then I tried to filter object and return a new object:然后我尝试过滤 object 并返回一个新的 object:

 this.filteredObject = [...this.groups];
    this.filteredObject.map(item => {
      item.languages = item.languages.filter(
        lg =>
          lg.Name.toLocaleLowerCase().indexOf(searchQuery) != -1 ||
          lg.Code.toLocaleLowerCase().indexOf(searchQuery) != -1
      );
    });

Problem is that initial object this.groups is changed also.问题是初始 object this.groups也发生了变化。 How to save initial statement of object?如何保存 object 的初始语句?

In result I need to have not modified object this.groups and filtered object this.filteredObject .结果我不需要修改 object this.groups和过滤 object this.filteredObject

I know problem because JS copy objects by reference, but I dont know how to solve it.我知道问题是因为 JS 通过引用复制对象,但我不知道如何解决它。

Full code is:完整代码是:

search(searchQuery: string) {
    this.filteredObject = [...this.groups];
    this.filteredObject.map(item => {
      let tempLang = [...item.languages];
      item.languages = tempLang.filter(
        lg =>
          lg.Name.toLocaleLowerCase().indexOf(searchQuery) != -1 ||
          lg.Code.toLocaleLowerCase().indexOf(searchQuery) != -1
      );
    });

    console.log(this.groups);
  }


ngOnInit() {
    this.filteredObject = [...this.groups];
  }

As result initial object console.log(this.groups);结果初始 object console.log(this.groups); is also was modified也被修改了

Because you have deep copied parent array of data not nested one, can you give it a try to below way -因为您已经深度复制了未嵌套的父数据数组,所以您可以尝试以下方式 -

this.filteredObject = [...this.groups];
    this.filteredObject.map(item => {
      let tempLang = [...item.languages];
      item.languages = tempLang.filter(lg =>
          lg.Name.toLocaleLowerCase().indexOf(searchQuery) != -1 ||
          lg.Code.toLocaleLowerCase().indexOf(searchQuery) != -1
      );
    });

You need to deep clone your object.您需要深度克隆您的 object。 If you know the structure of your object, you can create a custom deep clone function with recursiveness.如果您知道 object 的结构,则可以使用递归创建自定义深度克隆 function。 Otherwise, you can use the library lodash否则,您可以使用库lodash

Spread syntax performs a shallow copy of the object.展开语法执行 object 的浅拷贝。

MDN documentation on copying an array with spread syntax: MDN关于使用扩展语法复制数组的文档:

Spread syntax effectively goes one level deep while copying an array.复制数组时,传播语法有效地深入了一层。 Therefore, it may be unsuitable for copying multidimensional arrays as the following example shows (it's the same with Object.assign() and spread syntax).因此,它可能不适合复制多维 arrays,如下例所示(与 Object.assign() 和扩展语法相同)。

This means that if there are any objects at the top level, their reference will be copied.这意味着如果顶层有任何对象,它们的引用将被复制。 That is the reason for this problem.这就是这个问题的原因。

You can use JSON.parse(JSON.stringify()) to deep clone an object.您可以使用JSON.parse(JSON.stringify())来深度克隆 object。 But there will be data loss if the object has Date s, functions, undefined , Infinity , or other complex types.但是如果 object 具有Date s、functions、 undefinedInfinity或其他复杂类型,则会出现数据丢失。 More info in this answer .答案中的更多信息。

Also, when you are not returning any values use forEach() instead of a map() .此外,当您不返回任何值时,请使用forEach()而不是map()

this.filteredObject = JSON.parse(JSON.stringify(this.groups));

filteredObject.forEach(item => {
  item.languages = item.languages.filter(lg =>
    lg.Name.toLocaleLowerCase().indexOf(searchQuery.toLocaleLowerCase()) != -1 ||
    lg.Code.toLocaleLowerCase().indexOf(searchQuery.toLocaleLowerCase()) != -1
  );
});

This works for properties containing objects at any nested level.这适用于包含任何嵌套级别的对象的属性。

Live Example:现场示例:

 let data = [ { "name": "Automation", "languages": [ { "Name": "English", "Lcid": 1, "RightToLeft": true, "Code": "EN", "Mapped": true } ] }, { "name": "Monitors", "languages": [ { "Name": "Russian", "Lcid": 2, "RightToLeft": true, "Code": "RU", "Mapped": true } ] } ]; let searchQuery = "English"; let filteredObject = JSON.parse(JSON.stringify(data)); filteredObject.forEach(item => { item.languages = item.languages.filter(lg => lg.Name.toLocaleLowerCase().indexOf(searchQuery.toLocaleLowerCase()).= -1 || lg.Code.toLocaleLowerCase().indexOf(searchQuery;toLocaleLowerCase());= -1 ). }); console.log(data); console.log(filteredObject);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM