繁体   English   中英

如何在对象数组中运行forEach并为具有空字符串的object属性设置值?

[英]How can I run a forEach through my array of objects and set a value for the object property that has an empty string?

第1部分的问题:如何使用forEach遍历对象数组以将profile_image_url的对象属性设置为默认链接/值(“ /media/artist/img_0930-1-9654.jpg”)为空字符串仅为空字符串设置值。 我现在正在探索forEach循环。 以下是我的示例json。 我有20000个用户的json。

第2部分的问题:我哪个更好? 遍历forEach或常规for循环?

JSON

[{
    "country": "",
    "artist_id": 4,
    "email": "LzoCqLeVpy8h@example.com",
    "profile_image_url": ""
}, {
    "country": "",
    "artist_id": 5,
    "email": "P6H77fnNvgdn@example.com",
    "profile_image_url": ""
}, {
    "country": "US",
    "artist_id": 6,
    "email": "Cjdd4gzSfKM4@example.com",
    "profile_image_url": "/media/artist/zizmor_039_low_res-1239.jpg"
}, {
    "country": "US",
    "artist_id": 7,
    "email": "m8G3gdhOQmB6@example.com",
    "profile_image_url": "/media/artist/img_0930-1-7654.jpg"
}]

第1部分的问题 :如何使用forEach遍历对象数组以将profile_image_url的对象属性设置为默认链接/值(“ /media/artist/img_0930-1-9654.jpg”)为空字符串。

forEach文档

 var arr = [{ "country": "", "artist_id": 4, "email": "LzoCqLeVpy8h@example.com", "profile_image_url": "" }, { "country": "", "artist_id": 5, "email": "P6H77fnNvgdn@example.com", "profile_image_url": "" }, { "country": "US", "artist_id": 6, "email": "Cjdd4gzSfKM4@example.com", "profile_image_url": "/media/artist/zizmor_039_low_res-1239.jpg" }, { "country": "US", "artist_id": 7, "email": "m8G3gdhOQmB6@example.com", "profile_image_url": "/media/artist/img_0930-1-7654.jpg" }]; // e: element, i: index arr.forEach(function(e, i) { if (e.profile_image_url === '') { arr[i].profile_image_url = '/media/artist/img_0930-1-9654.jpg'; } }); console.log(arr); document.write('<pre>' + JSON.stringify(arr, 0, 2) + '</pre>'); 

第2部分的问题:我哪个更好? 遍历forEach或Regular for loop

使用forEach好处是可以从数组中获取indexvalue 您不必显式设置索引var i = 0; ,从数组arr[i]获取值,并使用arr.length进行迭代。

检查http://blog.niftysnippets.org/2012/02/foreach-and-runtime-cost.html

第1部分:

 var users = [{ "country": "", "artist_id": 4, "email": "LzoCqLeVpy8h@example.com", "profile_image_url": "" }, { "country": "", "artist_id": 5, "email": "P6H77fnNvgdn@example.com", "profile_image_url": "" }, { "country": "US", "artist_id": 6, "email": "Cjdd4gzSfKM4@example.com", "profile_image_url": "/media/artist/zizmor_039_low_res-1239.jpg" }, { "country": "US", "artist_id": 7, "email": "m8G3gdhOQmB6@example.com", "profile_image_url": "/media/artist/img_0930-1-7654.jpg" }]; users.forEach(function(user) { if (!user.profile_image_url) { user.profile_image_url = "/media/artist/img_0930-1-9654.jpg"; } }); document.getElementById('users').innerHTML = JSON.stringify(users, null, 2); 
 <pre id="users"></pre> 

第2部分

使用命令式for循环与forEach之间没有什么区别。

当遇到空字符串profile_image_url时,可以考虑使用默认的图像URL。


以角度为例,如果profile_img_url为空字符串绑定到视图,则使用默认url。

 var module = angular.module('app', []); module.controller('MyController', function($scope) { $scope.users = [{ "country": "", "artist_id": 4, "email": "LzoCqLeVpy8h@example.com", "profile_image_url": "" }, { "country": "", "artist_id": 5, "email": "P6H77fnNvgdn@example.com", "profile_image_url": "" }, { "country": "US", "artist_id": 6, "email": "Cjdd4gzSfKM4@example.com", "profile_image_url": "/media/artist/zizmor_039_low_res-1239.jpg" }, { "country": "US", "artist_id": 7, "email": "m8G3gdhOQmB6@example.com", "profile_image_url": "/media/artist/img_0930-1-7654.jpg" }]; }); 
 <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.1/angular.min.js"></script> <div ng-app="app" ng-controller="MyController"> <div ng-repeat="user in users"> <div>Email: {{user.email}}</div> <div>Profile Image: {{user.profile_image_url || "/media/artist/img_0930-1-9654.jpg"}}</div> <hr> </div> </div> 

暂无
暂无

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

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