[英]Append property to each object within array
Good morning all,大家早上好,
So here I asked myself the question to see if I was optimizing this rating there but I feel like I'm doing the wrong thing.所以在这里我问自己这个问题,看看我是否在优化这个评级,但我觉得我做错了。
So let me explain.所以让我解释一下。
We recover an array with objects, example of users.我们恢复了一个包含对象的数组,例如用户。
Who would give this:谁会给这个:
[
{
'name': 'john',
'age': '25'
},
{
'name': 'Doe',
'age': '28'
},
...
]
But in vuejs or in javascript or other, I find myself sometimes adding an example value: "'edit': false", but here to add it to all my objects in my table I pass the table with an each and i add this value like that.但是在 vuejs 或 javascript 或其他中,我发现自己有时会添加一个示例值:“'edit':false”,但在这里要将其添加到我表中的所有对象中,我使用 each 传递表并添加此值像那样。 but I don't find it clean would you have another tip or just how do you do it?但我觉得它不干净你有另一个提示吗?或者你是怎么做的?
[
{
'name': 'john',
'age': '25',
'edit': false
},
{
'name': 'Doe',
'age': '28',
'edit': false
},
...
]
i do like that personally我个人很喜欢
$.each(this.users, function(key){
Vue.set(this, 'edit', false)
});
Thank you for your answers I wish you a good day and above all good health.感谢您的回答,祝您有美好的一天,最重要的是身体健康。 Sorry about my bad english!抱歉我的英语不好!
If you use ES6, you can do one liner like that:如果你使用 ES6,你可以做一个这样的衬垫:
var users = [
{ name: 'John', age: 25 },
{ name: 'Jane', age: 28 }
];
users.map(user => user.edit = false);
Use map and spread operator使用 map 和 spread 运算符
const users = [
{ name: 'John', age: 25 },
{ name: 'Jane', age: 28 }
];
const usersUpdated = users.map(user => ({ ...user, edit: false }));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.