简体   繁体   English

如何将一堆数组转换为单个对象?

[英]How to convert a bunch of arrays into a single object?

Here is what im doing: 我在做什么:

onClick, grab details immediate subnodes and publish it on html. onClick,获取详细的直接子节点并将其发布在html上。 Status = DONE // This works well 状态=完成//效果很好

NOW, I am using a bunch of arrays to get this done. 现在,我正在使用一堆数组来完成此操作。

node.eachSubnode(function(node) {
                   title[title.length] = node.name; // This is what i want to modify
                   data[data.length] = node.data; // This is what i want to modify
                });

Here is how they look currently: 它们当前的外观如下:

title = ['Coffee', 'Tea'];
data = ["Americans", "Britishers"]; // i use a loop to iterate through these arrays and append to html.

Here is what i want it to be: 这就是我想要的:

var preference = {
title: 'Coffee',
data: 'Americans'
},
{
title: 'Tea',
data: 'Americans
}

I want to create this using the node.eachSubnode loop. 我想使用node.eachSubnode循环创建它。

I'm not sure if I understood correctly, but I think this is what you want: 我不确定我是否理解正确,但是我认为这是您想要的:

var preferences = [];

node.eachSubnode(function(node) {
    preferences.push({
        title: node.name,
        data: node.data.germ
    });
});

You cannot create an object that looks exactly like that, I think you need an array with objects. 您不能创建看起来像这样的对象,我认为您需要一个包含对象的数组。 Assuming your node is an array with the length property, this method is the fastest . 假设您的节点是具有length属性的数组,则此方法最快

var preference = new Array(node.length||0), i = 0;
node.eachSubnode(function(node) {
    preference[i++] = {
        title: node.name,
        data: node.data.germ
    };
});

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

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