简体   繁体   English

(AngularJS)全局替换数组中值的最有效方法

[英](AngularJS) Most efficient way to replace values in array globally

I want to replace all values in my array, here's the code: 我想替换数组中的所有值,这是代码:

var jsonImg = [
{"url":"https://example.amazonaws.com/images-lib/1496846856-Retina 01.jpg"},
{"url":"https://example.amazonaws.com/images-lib/1496846856-Retina 02.jpg"},
{"url":"https://example.amazonaws.com/images-lib/1496846856-Retina 03.jpg"}
];

var imgUrl = JSON.stringify(jsonImg);
var oldString = imgUrl;
var newString = oldString.replace(/https:\/\/example.amazonaws.com\/images-lib\//g,"cordova.url/");

$scope.Images = JSON.parse(newString);

But I think JSON.Stringfly and JSON.Parse are a bit redundant here, Do you think this is the most efficient way to replace all of the value JSON.Stringfly and JSON.Parse? 但是我认为JSON.Stringfly和JSON.Parse在这里有点多余,您认为这是替换所有值JSON.Stringfly和JSON.Parse的最有效方法吗? are there some other alternative to do this? 还有其他替代方法吗? Please check the fiddle here: https://jsfiddle.net/dedenbangkit/zu7utr6a/3/ 请在这里检查小提琴: https : //jsfiddle.net/dedenbangkit/zu7utr6a/3/

You are right. 你是对的。 There is no need to convert to string and back. 无需转换为字符串再返回。 Though when it comes to small number of values in array, it wouldn't matter much in terms of efficiency. 虽然涉及数组中的少量值,但就效率而言并没有多大关系。 But, why convert to string and back when you can have such powerful functions for array itself. 但是,当您可以为数组本身提供如此强大的功能时,为什么还要转换为字符串并返回。

Something like: 就像是:

$scope.Images = jsonImg.map(function(obj) {
  obj.url = "cordova.url" + obj.url.substr(obj.url.lastIndexOf("/"))
  return obj;
});

updated fiddle 更新的小提琴

In my understanding, repeating cdn path in all image urls is redundant. 以我的理解,在所有图像URL中重复CDN路径是多余的。 You should create another variable that will hold this path and when you need to use it, just concat both strings. 您应该创建另一个保存该路径的变量,并且在需要使用它时,只需连接两个字符串即可。 This way, you can easily change cdn path and you do not need to process. 这样,您可以轻松更改CDN路径,而无需进行处理。

Sample 样品

For demonstration purpose, I have used Math.random , but you can put your condition. 出于演示目的,我使用了Math.random ,但是您可以设置条件。

 function ImgCtrl($scope) { var s3 = "https://s3-ap-southeast-1.amazonaws.com/publixx-statics/images-lib/"; var cordava = "cordova.url/"; var urls = [{ "url": "1496846856-Retina 01.jpg" }, { "url": "1496846856-Retina 02.jpg" }, { "url": "1496846856-Retina 03.jpg" }] $scope.path = Math.floor(Math.random() * 100) % 2 === 0 ? s3 : cordava; $scope.Images = urls; console.log($scope.Images[0].url) } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script> <div ng-app> <div ng-controller="ImgCtrl"> <ul> <li ng-repeat="Image in Images"> {{path}}{{Image.url}} </li> </ul> </div> </div> 


As per our discussion, you can have 2 options: 根据我们的讨论,您可以有2个选择:

  • Sample 1 is more useful when your flag is more dynamic and will change very frequently. 当您的标志更加动态并且将非常频繁地更改时,示例1更有用。
  • Sample 2 is more useful in cases your flag will only have 1 value for 1 cycle. 如果您的标志在1个周期内只有1个值,则样本2更有用。 Something like A/B testing. 类似于A / B测试。 Since it will remain same always for a cycle, computing again makes no sense. 由于它将始终保持一个周期不变,因此再次进行计算是没有意义的。 Just save a processed value. 只需保存处理后的值即可。 This will also encapsulate urls in scope and it will not be available outside, so adds security. 这还将URL封装在范围内,并且将无法在外部使用,因此增加了安全性。
// Sample 1
var flag = Math.floor(Math.random() * 100) % 2 === 0;

function getImagePath(image) {
  var s3 = "https://s3-ap-southeast-1.amazonaws.com/publixx-statics/images-lib/";
  var cordava = "cordova.url/";
  return (flag ? s3 : cordava) + image;
}

// Usage:

var url = getImagePath(imageUrl)

// Sample 2
var path = (function() {
  var s3 = "https://s3-ap-southeast-1.amazonaws.com/publixx-statics/images-lib/";
  var cordava = "cordova.url/";
  return flag ? s3 : cordava;
})()

var url = path + imageUrl;
var jsonImg = [
  { 'url': 'https://example.amazonaws.com/images-lib/1496846856-Retina 01.jpg' },
  { 'url': 'https://example.amazonaws.com/images-lib/1496846856-Retina 02.jpg' },
  { 'url': 'https://example.amazonaws.com/images-lib/1496846856-Retina 03.jpg' },
];

jsonImg.map(function(image) {
  var split = image.url.split('/');
  var fileName = split[split.length - 1];
  image.url = 'cordova.url/' + fileName;
});

This will mutate all strings in your jsonImg array, replacing the https://example.amazonaws.com/images-lib/ URL's with cordova.url/ . 这将使您的jsonImg数组中的所有字符串发生突变,将https://example.amazonaws.com/images-lib/ URL替换为cordova.url/

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

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