2017-08-15 111 views
0

我需要通過對象列表來查找元素並向根添加新元素,我可以滾動列表並找到元素,但無法將其添加到正確的級別爲對象添加元素

var data = [ 

    { 
     "id": 1 
    }, 
    { 
     "id": 2 
    }, 
    { 
     "id": 3 
    }, 
    { 
     "id": 4, 
     "children": [ 
      { 
       "id": 6 
      }, 
      { 
       "id": 7      
      } 
     ] 
    }, 
    { 
     "id": 5 
    } 

]; 

function findById(data, id, element) { 
    function iter(a) { 
     if (a.id === id) { 
      a.push(element); // ERROR 
      result = a; 
      return true; 
     } 
     return Array.isArray(a.children) && a.children.some(iter); 
    } 
    var result; 
    data.some(iter); 
    return result 
} 


var element = { 
    "children": [{"id": 6}] 
}; 

findById(data, 5, element); 

document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>'); 

https://jsfiddle.net/4nrsccnu/

+1

a.push不做任何事情,因爲它不是數組。嘗試推入陣列。 –

+1

'a.push'拋出錯誤,因爲'a'是不包含數組的對象。您需要使用'Array.indexOf'在'data'中獲取'a'的位置。然後參考:https://stackoverflow.com/questions/586182/how-to-insert-an-item-into-an-array-at-a-specific-index –

+0

你可能試圖做到這一點,雖然:' a.children.push'而不是'a.push'這會在你的示例中使id爲5的對象具有id爲6的元素的子元素。 –

回答

1

你不能push,因爲a是一個對象。但是,您可以簡單地添加所需的屬性(在您的案例中,children),然後爲其分配一個值(在您的情況下,爲element)。

var data = [ 
 

 
    { 
 
     "id": 1 
 
    }, 
 
    { 
 
     "id": 2 
 
    }, 
 
    { 
 
     "id": 3 
 
    }, 
 
    { 
 
     "id": 4, 
 
     "children": [ 
 
      { 
 
       "id": 6 
 
      }, 
 
      { 
 
       "id": 7      
 
      } 
 
     ] 
 
    }, 
 
    { 
 
     "id": 5 
 
    } 
 

 
]; 
 

 
function findById(data, id, element) { 
 
    function iter(a) { 
 
     if (a.id === id) { 
 
      a.children = element; // Add property 'children' 
 
      result = a; 
 
      return true; 
 
     } 
 
     return Array.isArray(a.children) && a.children.some(iter); 
 
    } 
 
    var result; 
 
    data.some(iter); 
 
    return result 
 
} 
 

 
// remove property name from element, as that is being added in the function 
 
var element = [ 
 
    {"id": 6} 
 
]; 
 

 
findById(data, 5, element); 
 

 
document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>');

1

使用Object.assignelement對象的屬性合併到迭代的當前對象。

var data = [ 
 

 
    { 
 
     "id": 1 
 
    }, 
 
    { 
 
     "id": 2 
 
    }, 
 
    { 
 
     "id": 3 
 
    }, 
 
    { 
 
     "id": 4, 
 
     "children": [ 
 
      { 
 
       "id": 6 
 
      }, 
 
      { 
 
       "id": 7      
 
      } 
 
     ] 
 
    }, 
 
    { 
 
     "id": 5 
 
    } 
 

 
]; 
 

 
function findById(data, id, element) { 
 
    function iter(a) { 
 
     if (a.id === id) { 
 
      Object.assign(a, element); 
 
      result = a; 
 
      return true; 
 
     } 
 
     return Array.isArray(a.children) && a.children.some(iter); 
 
    } 
 
    var result; 
 
    data.some(iter); 
 
    return result 
 
} 
 

 

 
var element = { 
 
    "children": [{"id": 6}] 
 
}; 
 

 
findById(data, 5, element); 
 

 
console.log(data);

+0

我需要一個能夠掛載對象樹的結構,我通過這種方式可以傳遞父ID和要插入的元素,請您評估一下這是否是最好的方法。 https://jsfiddle.net/5ajxhwx3/2/ –

+1

這似乎很好。該函數的'element'參數與您在問題中顯示的內容不同。這意味着該函數只能添加子項,而不能添加其他屬性,但它可以將其他子項添加到已有子項的節點。 – Barmar

+0

是的,併爲此添加孩子和兄弟元素 –

1

push功能用於arrays,不反對。檢查詳細信息https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

如果要將單個密鑰對添加到對象中,可以使用Object.keysObject.values。像這樣:

function findById(data, id, element) { 
    function iter(a) { 
     if (a.id === id) { 
      var key = Object.keys(element)[0]; 
      var value = Object.values(element)[0]; 
      a[key] = value; 
      result = a; 
      return true; 
     } 
     return Array.isArray(a.children) && a.children.some(iter); 
    } 

    var result; 
    data.some(iter); 
    return result; 
}