Javascript 在不使用库的情况下遍历嵌套对象文字
嘿,伙计们,一般来说,我对javascript和Jquery都是新手,到目前为止,我对对象文本的使用很熟悉。现在,我正在浏览bootstrap modal.js插件的源代码,发现了以下几行代码:Javascript 在不使用库的情况下遍历嵌套对象文字,javascript,Javascript,嘿,伙计们,一般来说,我对javascript和Jquery都是新手,到目前为止,我对对象文本的使用很熟悉。现在,我正在浏览bootstrap modal.js插件的源代码,发现了以下几行代码: var options = $.extend({}, Modal.DEFAULTS, $this.data(), typeof option == 'object' && option) 您可以在上签出这行代码 现在,这行代码正在创建一个非常复杂的对象文字,在“object”中有很多“
var options = $.extend({}, Modal.DEFAULTS, $this.data(), typeof option == 'object' && option)
您可以在上签出这行代码
现在,这行代码正在创建一个非常复杂的对象文字,在“object”中有很多“object”,在“object”中有很多“object”,等等。。基本上是一个嵌套的对象文本
为了让我完全理解这行代码,我必须能够console.log对象文字
我看见一根线在上面
现在,我在答案中看到的打印对象文字的解决方案如下:
for (var key in validation_messages) {
if (validation_messages.hasOwnProperty(key)) {
var obj = validation_messages[key];
for (var prop in obj) {
// important check that this is objects own property
// not from prototype prop inherited
if(obj.hasOwnProperty(prop)){
alert(prop + " = " + obj[prop]);
}
}
}
}
var validation_messages = {
"key_1": {
"your_name": "jimmy",
"your_msg": "hello world"
},
"key_2": {
"your_name": "billy",
"your_msg": "foo equals bar"
}
}
var validation_messages = {
"key_1": {
"your_name": "jimmy",
"your_msg": "hello world"
},
"key_2": {
"your_name": {
"first_name" : "Ratan",
"secound_name" : "Tata",
},
"your_msg": "foo equals bar"
}
}
现在该解决方案运行良好,如果我有一个如下的对象文字:
for (var key in validation_messages) {
if (validation_messages.hasOwnProperty(key)) {
var obj = validation_messages[key];
for (var prop in obj) {
// important check that this is objects own property
// not from prototype prop inherited
if(obj.hasOwnProperty(prop)){
alert(prop + " = " + obj[prop]);
}
}
}
}
var validation_messages = {
"key_1": {
"your_name": "jimmy",
"your_msg": "hello world"
},
"key_2": {
"your_name": "billy",
"your_msg": "foo equals bar"
}
}
var validation_messages = {
"key_1": {
"your_name": "jimmy",
"your_msg": "hello world"
},
"key_2": {
"your_name": {
"first_name" : "Ratan",
"secound_name" : "Tata",
},
"your_msg": "foo equals bar"
}
}
但是,如果我有一个如下所示的对象文字:
for (var key in validation_messages) {
if (validation_messages.hasOwnProperty(key)) {
var obj = validation_messages[key];
for (var prop in obj) {
// important check that this is objects own property
// not from prototype prop inherited
if(obj.hasOwnProperty(prop)){
alert(prop + " = " + obj[prop]);
}
}
}
}
var validation_messages = {
"key_1": {
"your_name": "jimmy",
"your_msg": "hello world"
},
"key_2": {
"your_name": "billy",
"your_msg": "foo equals bar"
}
}
var validation_messages = {
"key_1": {
"your_name": "jimmy",
"your_msg": "hello world"
},
"key_2": {
"your_name": {
"first_name" : "Ratan",
"secound_name" : "Tata",
},
"your_msg": "foo equals bar"
}
}
我将在控制台中获得以下内容:
"your_name = jimmy"
"your_msg = hello world"
"your_name = [object Object]"
"your_msg = foo equals bar"
问题是每次一个键的值是一个对象,我希望循环进入该对象并从那里打印键和值,基本上我希望这是递归的。我试图修改我在该线程中发现的原始代码,但我几乎不知道我在做什么。下面是我修改的代码
var validation_messages = {
"key_1": {
"your_name": "jimmy",
"your_msg": "hello world"
},
"key_2": {
"your_name": {
"first_name" : "Ratan",
"secound_name" : "Tata",
},
"your_msg": "foo equals bar"
}
}
for (var key in validation_messages) {
if (validation_messages.hasOwnProperty(key)) {
var obj = validation_messages[key];
for (var prop in obj) {
// important check that this is objects own property
// not from prototype prop inherited
if(typeof prop != 'string') {
for(var obj_key in prop) {
var obj = prop[obj_key];
for (var prop in obj){
console.log(prop + " = " + obj[prop]);
}
}
}
if(obj.hasOwnProperty(prop)){
console.log(prop + " = " + obj[prop]);
}
}
}
}
为了重复我的问题,我必须理解如何递归地遍历对象文本并打印出其属性
我对对象文字数据显示的想法如下:
for (var key in validation_messages) {
if (validation_messages.hasOwnProperty(key)) {
var obj = validation_messages[key];
for (var prop in obj) {
// important check that this is objects own property
// not from prototype prop inherited
if(obj.hasOwnProperty(prop)){
alert(prop + " = " + obj[prop]);
}
}
}
}
var validation_messages = {
"key_1": {
"your_name": "jimmy",
"your_msg": "hello world"
},
"key_2": {
"your_name": "billy",
"your_msg": "foo equals bar"
}
}
var validation_messages = {
"key_1": {
"your_name": "jimmy",
"your_msg": "hello world"
},
"key_2": {
"your_name": {
"first_name" : "Ratan",
"secound_name" : "Tata",
},
"your_msg": "foo equals bar"
}
}
多谢各位
亚历山大 作为伪代码,解决方案非常简单:
function objectToString(object) {
string result = '';
for (key in object) {
if (hasFields(object[key]))
result += objectToString(object[key]);
else
result += key + ':' + object[key];
}
}
这里的额外困难是管理演示文稿。一种方法是引入缩进级别。然后,可以在每次调用时增加该级别,以获得所需的树表示
在JS中,您可能有如下代码:
function objectToString(object, level) {
var prettyString = '',
tabs = '';
// We prepare the indentation for the current object
for (var i=0; i<level; i++) {
tabs += '\t';
}
for (var key in object) {
prettyString += tabs + key;
// if the considered field is an object, we call this function with a
// greater indentation level
if (typeof object[key] === 'object') {
prettyString += '\n';
prettyString += prettyPrintKeys(object[key], level + 1);
} else {
prettyString += ':' + object[key] ;
}
prettyString += '\n';
}
return prettyString;
}
函数objectToString(对象,级别){
var prettyString='',
制表符=“”;
//我们为当前对象准备缩进
对于(var i=0;i可能是@harpreethingh的副本),那些家伙想要一个库,我不是!