简体   繁体   English

如何清除打字稿中数组中的元素值?

[英]How do I clear element values from an array in typescript?

Here I have an array. 在这里,我有一个数组。

  let dataList= [ { "VersionId": 475, "DocumentVersionFieldId": 1105, "VariantId": 4597, "ElementId": 1019, "FuelTypeId": 4, "VariantStructureWeek": "201817", "SalesVersion": "1", "MarketingCode": "1", "Option": "1", "SequenceNo": 1, "VariantExtendedValue": [], "cocVariantElementValues": [ { "VersionId": 475, "DocumentVersionFieldId": 1105, "VariantId": 4597, "ElementId": 1017, "ElementVariantId": 9548, "VariantValue": "uy", "IsRecordVisible": false, "cocElementLanguageDataCollection": [ { "ElementVariantId": 9548, "LanguageId": 0, "LanguageCode": "EN", "TranslationValue": "uy" }, { "ElementVariantId": 9548, "LanguageId": 0, "LanguageCode": "AL", "TranslationValue": "jh" }, { "ElementVariantId": 9548, "LanguageId": 0, "LanguageCode": "DE", "TranslationValue": "hj" }, { "ElementVariantId": 9548, "LanguageId": 0, "LanguageCode": "UR", "TranslationValue": "jh" } ] } ], "isRowDeleted": false, "operationIndicators": 0, "isMultiLanguage": true, "IsRecordVisible": true, "isShowUp": false, "IsNewRow": false } ] 


I want to clear value of the below elements from the above array. 我想从上述数组中清除以下元素的值。

  • I want to clear VariantValue element from cocVariantElementValues array 我想从cocVariantElementValues数组中清除VariantValue元素

  • I want to clear TranslationValue element from cocElementLanguageDataCollection array 我想从cocElementLanguageDataCollection数组中清除TranslationValue元素

This below is the expected one. 以下是预期的结果。

  [ { "VersionId": 475, "DocumentVersionFieldId": 1105, "VariantId": 4597, "ElementId": 1019, "FuelTypeId": 4, "VariantStructureWeek": "201817", "SalesVersion": "1", "MarketingCode": "1", "Option": "1", "SequenceNo": 1, "VariantExtendedValue": [], "cocVariantElementValues": [ { "VersionId": 475, "DocumentVersionFieldId": 1105, "VariantId": 4597, "ElementId": 1017, "ElementVariantId": 9548, "VariantValue": "",//here "IsRecordVisible": false, "cocElementLanguageDataCollection": [ { "ElementVariantId": 9548, "LanguageId": 0, "LanguageCode": "EN", "TranslationValue": "",//here }, { "ElementVariantId": 9548, "LanguageId": 0, "LanguageCode": "AL", "TranslationValue": "",//here }, { "ElementVariantId": 9548, "LanguageId": 0, "LanguageCode": "DE", "TranslationValue": "",//here }, { "ElementVariantId": 9548, "LanguageId": 0, "LanguageCode": "UR", "TranslationValue": "",//here } ] } ], "isRowDeleted": false, "operationIndicators": 0, "isMultiLanguage": true, "IsRecordVisible": true, "isShowUp": false, "IsNewRow": false } ] 

Is there a straightforward way, or do I need to loop through it and remove them manually? 有没有简单的方法,还是我需要遍历它并手动将其删除?

You can't do this without looping over the all elements, however there are some shortcuts you can use instead of for: 您不能在不遍历所有元素的情况下执行此操作,但是可以使用一些快捷方式代替:

array.map(function(x) { 
  x['VariantValue'] = ''; 
  return x
});

Alternatively you can use this too, it is es6 fat arrow function 或者您也可以使用此功能,它是es6胖箭头功能

array.map(x=>x['VariantValue']='');

There might be some syntax errors in the above code as it is not tested. 由于未经测试,上述代码中可能存在一些语法错误。

thanks @ppl 谢谢@ppl

I got it by using multiple .map() for my scenario. 我通过为我的场景使用多个.map()来获得它。

dataList.map(function(x) { 
      x.cocVariantElementValues.map(function(y) { 
      y['VariantValue'] = ''; 
      y.cocElementLanguageDataCollection.map(function(z) { 
      z['TranslationValue'] = ''; 
      return z
    });
      return y;
    });
      return x;
    });

Successful Snippet 成功片段

 let dataList= [ { "VersionId": 475, "DocumentVersionFieldId": 1105, "VariantId": 4597, "ElementId": 1019, "FuelTypeId": 4, "VariantStructureWeek": "201817", "SalesVersion": "1", "MarketingCode": "1", "Option": "1", "SequenceNo": 1, "VariantExtendedValue": [], "cocVariantElementValues": [ { "VersionId": 475, "DocumentVersionFieldId": 1105, "VariantId": 4597, "ElementId": 1017, "ElementVariantId": 9548, "VariantValue": "uy", "IsRecordVisible": false, "cocElementLanguageDataCollection": [ { "ElementVariantId": 9548, "LanguageId": 0, "LanguageCode": "EN", "TranslationValue": "uy" }, { "ElementVariantId": 9548, "LanguageId": 0, "LanguageCode": "AL", "TranslationValue": "jh" }, { "ElementVariantId": 9548, "LanguageId": 0, "LanguageCode": "DE", "TranslationValue": "hj" }, { "ElementVariantId": 9548, "LanguageId": 0, "LanguageCode": "UR", "TranslationValue": "jh" } ] } ], "isRowDeleted": false, "operationIndicators": 0, "isMultiLanguage": true, "IsRecordVisible": true, "isShowUp": false, "IsNewRow": false } ] let result = dataList.map(function(x) { x.cocVariantElementValues.map(function(y) { y['VariantValue'] = ''; y.cocElementLanguageDataCollection.map(function(z) { z['TranslationValue'] = ''; return z }); return y; }); return x; }); console.log(result) 

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

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