简体   繁体   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?

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 for loop ? 遍历forEach或Regular for loop

Using forEach has benefits of getting index and value from the array. 使用forEach好处是可以从数组中获取indexvalue 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.

相关问题 如何在将某些属性设置为false的对象数组中找到下一个对象? - How can I find the next object in an array of objects that has a certain property set to false? 我如何遍历对象数组,为每个 object 调用 API,然后将响应设置为每个 object 中的值 - How can I iterate through an array of objects, make an API call for each object, then set the response as a value in each object 为什么不能在JavaScript中将对象属性设置为空字符串? - Why can't I set object property to empty string in JavaScript? 如何在数组中设置对象? - how can I set my objects in array? Javascript:如何为对象数组中的 object 属性设置值? - Javascript: How to set value to an object property inside an array of objects? 如何在具有对象的React App中循环遍历javascript数组并获得具有特定值的属性的计数 - How do I loop through a javascript Array in a React App that has object and get a count on a property that has a certain value 如何使用for循环在空对象中设置属性? - how can i set the property in empty object using for loop? JavaScript获得对象数组的值或属性(如果对象具有该属性) - JavaScript get value or property of an Array of objects if object has that property 如何循环遍历 object 数组并从数组中列出的两个对象中获取一个属性元素? - How can I loop through an array of object and get one property element out of the two objects listed inside the array? 我如何访问在react状态下设置的对象数组中的属性? - How can I access a property in an array of objects that is set in the state in react?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM