如何在javascript中将子元素添加到现有数组中

时间:2017-05-11 08:39:14

标签: javascript json

我在javascript中有一个数组:

var elements = [ // original hierarhical array to display
            {key:10, label:"Web Testing Dep.", open: true, children: [
                {key:20, label:"Elizabeth Taylor"},
                {key:30, label:"Managers",  children: [
                    {key:40, label:"John Williams"},
                    {key:50, label:"David Miller"}
                ]},
                {key:60, label:"Linda Brown"},
                {key:70, label:"George Lucas"}
            ]},
            {key:110, label:"Human Relations Dep.", open:true, children: [
                {key:80, label:"Kate Moss"},
                {key:90, label:"Dian Fossey"}
            ]}
        ];

如果我想添加一个全新元素,我可以使用push()。但我想添加一个元素的一部分。 例如,我想添加

var toAdd = {key:100, label:"Johny Dep"}

关键10" web测试部门的孩子们。"。 是否也可以使用push()

5 个答案:

答案 0 :(得分:1)

您可以使用迭代和递归方法来获取不插入新对象。

function addToStructure(structure, object, parent) {
    structure.some(function iter(a) {
        if (a.key === parent) {
            a.children = a.children || [];
            a.children.push(object);
            return true;
        }
        return Array.isArray(a.children) && a.children.some(iter);
    });
}

var elements = [{ key: 10, label: "Web Testing Dep.", open: true, children: [{ key: 20, label: "Elizabeth Taylor" }, { key: 30, label: "Managers", children: [{ key: 40, label: "John Williams" }, { key: 50, label: "David Miller" }] }, { key: 60, label: "Linda Brown" }, { key: 70, label: "George Lucas" }] }, { key: 110, label: "Human Relations Dep.", open: true, children: [{ key: 80, label: "Kate Moss" }, { key: 90, label: "Dian Fossey" }] }];

addToStructure(elements, { key: 100, label: "Johny Dep" }, 10);

console.log(elements);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

试试这个elements[0].children.push(toAdd)



var elements = [ // original hierarhical array to display
            {key:10, label:"Web Testing Dep.", open: true, children: [
                {key:20, label:"Elizabeth Taylor"},
                {key:30, label:"Managers",  children: [
                    {key:40, label:"John Williams"},
                    {key:50, label:"David Miller"}
                ]},
                {key:60, label:"Linda Brown"},
                {key:70, label:"George Lucas"}
            ]},
            {key:110, label:"Human Relations Dep.", open:true, children: [
                {key:80, label:"Kate Moss"},
                {key:90, label:"Dian Fossey"}
            ]}
        ];
var toAdd = {key:100, label:"Johny Dep"}
elements[0].children.push(toAdd)
console.log(elements)




答案 2 :(得分:0)

你可以这样做:

elements.filter(function(e) {
    return e.key === 10;
})[0].children.push(toAdd);

toAdd{ key: 100, label: "Johny Depp" }

的位置

答案 3 :(得分:0)

首先,您需要找到要修改的元素:

var element_to_modify = elements.find(function(element) {
  return element.key == 10;
});

然后,通过向他们的孩子推送新对象来修改项目:

element_to_modify.children.push(toAdd)

答案 4 :(得分:0)

正如CBroe在他的评论中建议我们可以推送数组中的元素或对象。这个数组本身是否是更大数据结构的一部分并不重要。

<强>样本

var elements = [ // original hierarhical array to display
            {key:10, label:"Web Testing Dep.", open: true, children: [
                {key:20, label:"Elizabeth Taylor"},
                {key:30, label:"Managers",  children: [
                    {key:40, label:"John Williams"},
                    {key:50, label:"David Miller"}
                ]},
                {key:60, label:"Linda Brown"},
                {key:70, label:"George Lucas"}
            ]},
            {key:110, label:"Human Relations Dep.", open:true, children: [
                {key:80, label:"Kate Moss"},
                {key:90, label:"Dian Fossey"}
            ]}
        ];
        
var toAdd = {key:100, label:"Johny Dep"}

elements[0].children.push(toAdd);

console.log(elements);