简体   繁体   English

如何在 javascript 数组上迭代新的 class 实例?

[英]How to iterate new class intances over a javascript array?

I have an array which looks like this:我有一个看起来像这样的数组:

var array = ["name", " zip", " city", " 51.408", "1.333", " 5.008", "name2", "zip2", "city2", " 51.404", "4.999", "2.434"]

It contains data from 2 users.它包含来自 2 个用户的数据。 I want to create 2 instances of a class below.我想在下面创建一个 class 的 2 个实例。

class User {
    constructor(name, zip, city, x, y) {
        this.name = name;
        this.zip = zip;
        this.city = city;
        this.x = x;
        this.y = y;
    }

The 6th and the last element of the array is not used in the class. class 中未使用数组的第 6 个和最后一个元素。

This is an iteration I wrote:这是我写的一个迭代:

for (var i = 0; i < array.length; i++) {

    var user1 = new User(arrayData[i], arrayData[i+1], arrayData[i+2], arrayData[i+3], arrayData[i+4], arrayData[i+5])
    var user2 = new User(arrayData[i+6], arrayData[i+7], arrayData[i+8], arrayData[i+9], arrayData[i+10], arrayData[i+11])

}

It works fine but as you can see this is not a really smart peace of code.它工作正常,但正如您所见,这并不是真正聪明的代码和平。 I want to make it better.我想让它变得更好。 Also, I don't know how to make it works for n number of users.另外,我不知道如何使它适用于 n 个用户。 Do you have any ideas?你有什么想法?

I think you are looking for我想你正在寻找

var users = [];
for (var i = 0; i < array.length; i += 6) {
//                                ^^^^^^
    var user = new User(arrayData[i], arrayData[i+1], arrayData[i+2], arrayData[i+3], arrayData[i+4], arrayData[i+5]);
    users.push(user);
}
console.log(users);

(Using i+5 < array.length for the loop condition would be more accurate, but doesn't matter as long as it's guaranteed the array length is always a multiple of 6) (使用i+5 < array.length作为循环条件会更准确,但只要保证数组长度始终是 6 的倍数就没有关系)

@trincot follows the same idea and achieves the same result with more concise code (mapping, slicing, spreading), I tried to keep it as simple as the original code. @trincot 遵循相同的想法,并使用更简洁的代码(映射、切片、扩展)实现相同的结果,我试图使其与原始代码一样简单。

Your loop should really only create one user in its body, and i should increment with steps of 6.您的循环实际上应该只在其主体中创建一个用户,并且i应该以 6 步递增。

You can also use a parameter spread to avoid you have to address the five argument separately.您还可以使用参数展开来避免必须分别处理五个参数。

Finally, with Array.from you can loop and create an array of users:最后,使用Array.from你可以循环并创建一个用户数组:

 class User { constructor(name, zip, city, x, y) { this.name = name; this.zip = zip; this.city = city; this.x = x; this.y = y; } } var array = ["name", " zip", " city", " 51.408", "1.333", " 5.008", "name2", "zip2", "city2", " 51.404", "4.999", "2.434"]; let users = Array.from({length: array.length / 6}, (_, i) => new User(...array.slice(i*6, i*6+5)) // Not including the unused 6th value ); console.log(users);

You could use reduce method with % remainder operator to get every nth element and then use slice with rest operator to get data for the class instance.您可以使用带有%余数运算符的reduce方法来获取每个nth元素,然后使用带有 rest 运算符的slice来获取 class 实例的数据。

 var array = ["name", " zip", " city", " 51.408", "1.333", " 5.008", "name2", "zip2", "city2", " 51.404", "4.999", "2.434"] class User { constructor(name, zip, city, x, y) { this.name = name; this.zip = zip; this.city = city; this.x = x; this.y = y; } } const users = array.reduce((r, e, i, a) => { if (i % 6 === 0) { const props = a.slice(i, i + 6); const user = new User(...props); r.push(user); } return r; }, []) console.log(users)

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

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