Alternatives for using forEeach() loop while converting data for D3.js

久未见 提交于 2019-12-04 14:58:59

If you're not sure whether you'll be getting an 'array of objects', or an 'objects with key/values', then I would use _.forEach in the library 'lodash' will iterate over an array of objects OR the key/values of an object.

Based on the error message, Cannot read property 'length' of undefined the error is not coming from dataset.forEach, it is coming from somewhere where you are looking up the .length of something.

(Note: in Chrome dev tools you can inspect/expand the error in the console and see what file and line number it comes from).

If it's coming from any of the code you shared, then this is the offending line:

var itemCount = data.Items.length;

If data.Items happens to be undefined, you'd get this error. So insert a console.log(data) on the line right before the error'ing line, inspect the output, look for data.Items, which is likely not there. Your diagnosis should point you in the direction to fixing it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!