简体   繁体   English

如何将数组的值添加到对象

[英]How do I add the value of an array to an object

I've got code which splits the key and value at the = sign at end if line, the problem is that I have lines which have more then one = 我有代码, if在行结束if将键和值分别在=符号处分开,问题是我有多行的行=

For example: 例如:

user = aaaa
userb = bbbb
userc = test
userd = foo = bar = test 

I read file with node fs and I divide it to the key and value at the = so userd causes a problem. 我用节点fs读取文件,然后将其分为键和值= userd导致问题。

I am using var array = line.split('='); 我使用的是var array = line.split('=');

Then I assign it to object in the loop 然后我将它分配给循环中的对象

myobj = {}

myobj[array[0]] = array[1];

This is working (my obj in the loop contains all the users) until I get to userD . 这是有效的(我的obj循环中包含所有用户),直到我到达userD So how can I concatenate all the values of userD to myObj 那么如何将userD所有值连接到myObj

If we assume that everything after the first = sign is the value, regardless of whether theres another = sign then you can do this: 如果我们假设第一个=符号之后的所有内容都是值,无论是否另一个=符号,那么你可以这样做:

myobj[array[0]] = array.slice(1).join(' = ');

Now, if you want to store it simple as an array of equal values, you can do: 现在,如果要将其简单存储为具有相等值的数组,则可以执行以下操作:

myobj[array[0]] = array.slice(1);

The slice function returns the same array starting at the element passed (it does more but thats what it's for here). slice函数从传递的元素开始返回相同的数组(它做的更多,但这就是它的用途)。

Update I didn't think of this answer but it's even a bit faster! 更新我没有想到这个答案,但它甚至更快!

Have a look at Array.prototype. shift() 看看Array.prototype. shift() Array.prototype. shift() : Array.prototype. shift()

myobj[array.shift()] = array;

Unlike Array.prototype. splice() Array.prototype. splice()不同Array.prototype. splice() Array.prototype. splice() , shift() won't create an entirely new array in order to wrap up the one value we're trying to extract. Array.prototype. splice()shift()不会创建一个全新的数组,以便包装我们试图提取的一个值。 See my comment here . 在这里看我的评论。

Here are the results from a performance test ; 以下是性能测试的结果; it shows operations per second (higher is better): 它显示每秒操作数(更高更好):

UserAgent                                       shift       slice       splice
Chrome 44.0.2403                                4,290,938   3,993,798   1,042,423
Chrome 46.0.2468                                3,631,277   4,020,712   1,098,866
Firefox 38.0                                    1,706,068   1,419,219   895,714
Firefox 40.0                                    1,739,110   1,508,623   955,794
IE 11 in Compatibility Mode 10.0.0              3,393,698   2,855,417   1,721,229
Total                                           14,761,091  13,797,769  5,714,026

According to these results, shift() is the fastest with slice() right behind. 根据这些结果, shift()是最快的, slice()就在后面。 splice() , on the other hand, is a good bit slower than both... mainly because it's ill-suited to this particular task. 另一方面, splice()比两者都慢一点......主要是因为它不适合这个特定的任务。

That said, I was surprised. 那就是说,我很惊讶。 I expected this answer to be the fastest. 我希望这个答案是最快的。

Use splice function: 使用拼接功能:

myobj[array.splice(0, 1)] = array;

This will cut the first item out of your array and return it as myobj parameter, the rest is going to be kept in the array, so you can either use array as an object property or let's say join it to a string: 这将从数组中删除第一项并将其作为myobj参数返回,其余的将保留在数组中,因此您可以将数组用作对象属性,或者让我们将其连接到字符串:

myobj[array.splice(0, 1)] = array.join(", ");

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

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