0

With a nested object such as the 'root' object below; How can I add a 'dummy' object to those arrays where the length of the array is less than 2.

var root = {
  children: [
    {
      children: [
        {value: 42}
      ]
    },
    {
      children: [
        {value: 42},
        {value: 42}
      ]
    },
    {value: 42}
  ]
};

An example of an object i want to insert into arrays where array.length < 2 :

var dummy = {value: 10, dummy: 1};

the resulting array:

var rootWithDummies = {
  children: [
    {
      children: [
        {value: 42},
        {value: 10, dummy: 1}
      ]
    },
    {
      children: [
        {value: 42},
        {value: 42}
      ]
    },
    {value: 42}
  ]
};

I am trying to use recursion though I still have much to learn: EDIT original addDummies function was checking for nest.values when it should have been nest.children CORRECTED FUNCTION

function addDummies (nest, dummyObject) {

    if (nest.hasOwnProperty("children")) {

        if (nest.children.length < 2) {
            nest.children.push(dummyObject)
        }

        nest.children.forEach(function (item) {

            addDummies(item);

        ;})
    }
}

OLD INCORRECT FUNCTION function addDummies (nest, dummyObject) {

    if (nest.hasOwnProperty("values")) {

        if (nest.values.length < 2) {
            nest.values.push(dummyObject)
        }

        nest.values.forEach(function (item) {

            addDummies(nest.values);

        ;})
    }
}

this attempt does not seem to call addDummies recursively nor does it have any checking that the value of 'values' is actually an array (and therefore has a length property).

4

2 回答 2

0

Here you have a working solution:

var root = {
  children: [
    {
      children: [
        {value: 42}
      ]
    },
    {
      children: [
        {value: 42},
        {value: 42}
      ]
    },
    {value: 42}
  ]
};
var dummy = {value: 10, dummy: 1};

function addDummies (node) {

    if (node.children) {
      if (node.children.length < 2) {
        node.children.push(dummy)
      }  
      for (var i=0, len=node.children.length; i<len; i++) {
        addDummies(node.children[i]);
      }
    }
}

addDummies(root);
console.log(root);

​ And the js fiddle: http://jsfiddle.net/kKrFC/

I'll edit with an explanation of why it wasn't working for you in an hour or so, I have to go now.

EDIT

The hasOwnProperty check is not necessary, as you only need to check it when you are looping through the object's members. What you need to do is send as the function parameter each of the elements in children, as each one of this is like a root node. That is precisely the idea of recursion (at least in this case).

于 2012-09-10T12:51:09.217 回答
0

You were close; Changes:

  • remove the syntax error ;}) on the third to last line
  • use a while loop instead of appending just one dummy object

http://jsfiddle.net/jbabey/V3bvV/

var padNode = function padNode (node) {
    if (node.children) {
        while (node.children.length < 2) {
            node.children.push(dummy);
        }  

        for (var i = 0; i < node.children.length; i++) {
            padNode(node.children[i]);
        }        
    }
};
于 2012-09-10T13:02:32.840 回答