简体   繁体   English

如何在javascript中操纵数组的值?

[英]How do I manipulate a value of an array in javascript?

I'm quite new to Javascript and I have the following javascript array in an AJAX Request that contains the following: 我是Java的新手,我在AJAX请求中具有以下javascript数组,其中包含以下内容:

["12435|#CANON#DEVICE#|#50#|Machine Detail|Details|SampleRow|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE"]

I wanna manipulate the TRUE and FALSE value. 我想操纵TRUE和FALSE值。 If they're in uppercase, I want to make it lowercase. 如果它们是大写的,我想将其小写。 Any idea how I can do it? 知道我该怎么做吗?

If you want to modify the list you could just loop through all of its items, modify the value and set it to the same index of the list. 如果要修改列表,则可以循环浏览其所有项目,修改值并将其设置为列表的相同索引。 (You don't need to set it if you are dealing with objects). (如果要处理对象,则无需设置它)。

 var list = ["12435|#CANON#DEVICE#|#50#|Machine Detail|Details|SampleRow|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE"]; list.forEach(function(item, index) { list[index] = item.replace(/(TRUE|FALSE)/g, function(upperCase) { return upperCase.toLowerCase(); }); }); console.log(list); 

Same thing using a for loop: 使用for循环也是一样:

 var list = ["12435|#CANON#DEVICE#|#50#|Machine Detail|Details|SampleRow|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE"]; for (var index = 0; index < list.length; index++) { list[index] = list[index].replace(/(TRUE|FALSE)/g, function(upperCase) { return upperCase.toLowerCase(); }); } console.log(list); 

If you want to create a copy you could do: 如果要创建副本,可以执行以下操作:

 var list = ["12435|#CANON#DEVICE#|#50#|Machine Detail|Details|SampleRow|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE"]; var newList = list.map(function(item) { return item.replace(/(TRUE|FALSE)/g, function(upperCase) { return upperCase.toLowerCase(); }); }); console.log(newList); 

The above scripts will also transform something like ["THIS IS NOT TRUE|TRUE|FALSE"] to ["THIS IS NOT true|true|false"] . 上面的脚本还会将["THIS IS NOT TRUE|TRUE|FALSE"]["THIS IS NOT true|true|false"] If you do not want that you should use this regex instead /(^|(?<=\\|))(TRUE|FALSE)(\\||$)/ ie: 如果您不希望使用此正则表达式,请改用/(^|(?<=\\|))(TRUE|FALSE)(\\||$)/即:

 var list = ["12435|#CANON#DEVICE#|#50#|Machine Detail|Details|SampleRowFALSE|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE"]; for (var index = 0; index < list.length; index++) { list[index] = list[index].replace(/(^|(?<=\\|))(TRUE|FALSE)(\\||$)/g, function(upperCase) { return upperCase.toLowerCase(); }); } console.log(list); 

Just use replace with map : 只需使用map replace

 const arr = ["12435|#CANON#DEVICE#|#50#|Machine Detail|Details|SampleRow|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE"]; const res = arr.map(e => e.replace(/(TRUE|FALSE)/g, m => m.toLowerCase())); console.log(res); 

 const arrayString = ["12435|#CANON#DEVICE#|#50#|Machine Detail|Details|SampleRow|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE"] const arrayOfValues = arrayString[0].split('|').map(val => { if(val === 'TRUE' || val === 'FALSE') { return val.toLowerCase(); } else { return val; } }); console.log(arrayOfValues) 

Use RegEx as you have been told before. 如前所述,使用RegEx。 If you want to learn more about this look at: W3Schools 如果您想了解更多有关此的信息,请访问: W3Schools

One solution could be like this: 一种解决方案可能是这样的:

  var ajaxResponse = "12435|#CANON#DEVICE#|#50#|Machine Detail|Details|SampleRow|FALSE|FALSE|FALSE|FALSE|FALSE|FALSE|TRUE"; ajaxResponse = ajaxResponse.replace(/FALSE/g, "false"); ajaxResponse = ajaxResponse.replace(/TRUE/g, "true"); console.log(ajaxResponse); 

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

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