简体   繁体   English

Javascript将数组写入字符串以在函数中使用

[英]Javascript write array to string for use in function

The following function gets a few variables from a table and puts them in an array: 以下函数从表中获取一些变量并将它们放入数组中:

var data = [];

function getInfo() {
    var tds = null;
    $('tr.dirRow').each(function () {
        // Get child <td>s of each .dirRow class <tr>s
        var tds = $(this).children();

        // Get/sanitize information for new table
        var fullName = tds.eq(1).text();
        var org = tds.eq(4).text(); //other variables...

        // Push data
        if (fullName) {
            data.push({
                "id": fullName,
                "org": org //other variables...
            });
        }
    });

}

I have a function that creates an org chart: 我有一个创建组织结构图的函数:

function createOrgChart() {
        var peopleElement = document.getElementById("people");
        var orgChart = new getOrgChart(peopleElement, {
            theme: "sara",
            dataSource: document.getElementById("orgChartData"),
            customize: {
                //***Need help getting this information in this spot***
                // "id": { color: "green" }, <--this is what the data should be formatted like
            }
        });

    }

I can correctly console.log() the information I want in the commented section above, based on the for() loop below, but I can't figure out how to output to the "customize" section, in the format it needs to be. 我可以基于下面的for()循环正确地在上面的注释部分中console.log()我想要的信息,但是我无法弄清楚如何以所需的格式输出到“ customize”部分是。

Code that correctly logs the output I desire in the "customize: {" section of the function above: 上面的函数的“ customize:{”部分中正确记录了我想要的输出的代码:

for (var i = 0; i < data.length; i++) {
    if (data[i].org.substring(0, 4) == "Dep1") {
        console.log("\"" + data[i].id + "\": { color: \"green\"},");
        //results in:
        //"id1": { color: "green" },
    } else if (data[i].org.substring(0, 4) == "Dep2") {
        console.log("\"" + data[i].id + "\": { color: \"red\"},");
        //results in:
        //"id2": { color: "red" },
    }
}

Data array, as requested in comments: 数据数组,如注释中所要求:

 [Object { id="First Last",  parentID="Supervisor Name",  org="Dep1",  more...}, Object { id="First2 Last2",  parentID="Supervisor2",  org="Dep2",  more...}, //more objects 

Final code that worked for me if it helps anyone else: 如果对其他人有用,则对我有用的最终代码:

var data = [];
var customize = {};
var orgColors = {
    'Dep1': { color: "blue" },
    'Dep2': { color: "green" },
    'Dep3': { color: "grey" }
}

$(document).ready(function () {
    getInfo();
    createOrgChart();
});

function getInfo() {
    var tds = null;
    $('tr.dirRow').each(function () {
        // Get child <td>s of each .dirRow class <tr>s
        var tds = $(this).children();

        // Get/sanitize information for new table
        var fullName = tds.eq(1).text();
        var org = tds.eq(4).text(); //other variables...

        // Push data
        if (fullName) {
            data.push({
                "id": fullName,
                "org": org //other variables...
            });
        }

        customize[fullName] = orgColors[org.substring(0, 4)];

    });

}
function createOrgChart() {
    var peopleElement = document.getElementById("people");
    var orgChart = new getOrgChart(peopleElement, {
        theme: "sara",
        dataSource: document.getElementById("orgChartData"),
        customize: customize
    });

}

It's hard to tell exactly what you want, but here's the sort of thing you might try: 很难确切地说出您想要什么,但是您可以尝试以下操作:

var orgFormats = {
    'Dep1': { color: "green"},
    'Dep2': { color: "red"}
}

// then later, when you know what personData is

var customize = {};
customize[personData.id] = orgFormats[personData.org.substring(0, 4)];

var configObject = {
    theme: 'sara',
    dataSource: document.getElementById("orgChartData"),
    customize: customize
};

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

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