[英]How can I run a forEach through my array of objects and set a value for the object property that has an empty string?
Part 1 question: How can I use forEach to loop through my array of objects to set the object property of profile_image_url which has an empty string to a default link/value("/media/artist/img_0930-1-9654.jpg").Set the value for only empty strings. 第1部分的问题:如何使用forEach遍历对象数组以将profile_image_url的对象属性设置为默认链接/值(“ /media/artist/img_0930-1-9654.jpg”)为空字符串仅为空字符串设置值。 I am exploring forEach loop right now.
我现在正在探索forEach循环。 Below is my sample json.
以下是我的示例json。 I have a json of 20000 users.
我有20000个用户的json。
Part 2 question: Which one am I better off? 第2部分的问题:我哪个更好? Iterate through forEach or regular for loop?
遍历forEach或常规for循环?
JSON 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"
}]
Part 1 question : How can I use forEach to loop through my array of objects to set the object property of
profile_image_url
which has an empty string to a default link/value("/media/artist/img_0930-1-9654.jpg").第1部分的问题 :如何使用forEach遍历对象数组以将
profile_image_url
的对象属性设置为默认链接/值(“ /media/artist/img_0930-1-9654.jpg”)为空字符串。
forEach documentation 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>');
Part 2 question: Which one am I better off?
第2部分的问题:我哪个更好? Iterate through
forEach
or regular forloop
?遍历
forEach
或Regular forloop
?
Using forEach
has benefits of getting index
and value
from the array. 使用
forEach
好处是可以从数组中获取index
和value
。 You don't have to explicitly set the index var i = 0;
您不必显式设置索引
var i = 0;
, get the value from array arr[i]
and use arr.length
for iteration. ,从数组
arr[i]
获取值,并使用arr.length
进行迭代。
Check http://blog.niftysnippets.org/2012/02/foreach-and-runtime-cost.html 检查http://blog.niftysnippets.org/2012/02/foreach-and-runtime-cost.html
Part 1: 第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>
Part 2 第2部分
There won't be much difference if any between using an imperative for loop vs forEach. 使用命令式for循环与forEach之间没有什么区别。
The alternative you can consider is defaulting to that image url when you encounter an empty string as a profile_image_url
. 当遇到空字符串
profile_image_url
时,可以考虑使用默认的图像URL。
Using angular as an example, when binding to a view if the profile_img_url
is an empty string use the default 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.