简体   繁体   English

如何显示对象中的所有内容,包括嵌套对象和数组?

[英]How to display everything from an object including nested objects and arrays?

I have an object which have nested objects and an array.我有一个具有嵌套对象和数组的对象。 I'm trying to console.log all the data including the data inside the nested object but failing to do so.我正在尝试console.log所有数据,包括嵌套对象内的数据,但没有这样做。 The result I'm getting shows all the data except the ones which are objects.我得到的结果显示了除对象之外的所有数据。 I get [Object object] instead of those.我得到[Object object]而不是那些。 What I want is to display everything in the console in a format which is not an object or JSON(like the result below, except I want to display the values of f and g as well).我想要的是在控制台中以一种不是对象或 JSON 的格式显示所有内容(如下面的结果,除了我还想显示 f 和 g 的值)。

This is the object:这是对象:

let data = { 
    a: 'Tesla',
    b: 'Mclaren',
    c: 'Ferrari',
    d: 'Lamborghini',
    e: 'Lotus',
    'f':{ 
        name: 'John',
        'e-mail': 'xyz@example.com',
        phone: '+12345678',
        country: 'USA',
        car: 'Toyota Prius' 
    },
    'g':{ 
        name: 'Sophie',
        'e-mail': 'xyz@example.com',
        phone: '+12345678',
        country: 'UK',
        car: 'Nissan Bluebird' 
    },
    h: 'Volkswagen',
    i: 'Bugatti',
    j:[ 
        '% mileage',
        '% top speed',
        '% suspension',
        '% navigation',
        '% horsepower',
        '% 0-60s' 
    ] 
}

This is the code I tried:这是我试过的代码:

for(var key in data){
    console.log(key + " : " + data[key]);
}

This is the result I'm getting:这是我得到的结果:

a : Tesla
b : Mclaren
c : Ferrari
d : Lamborghini
e : Lotus
f : [object Object]
g : [object Object]
h : Volkswagen
i : Bugatti
j : % mileage,% top speed,% suspension,% navigation,% horsepower,% 0-60s

console.log(JSON.parse(JSON.stringify(data))) :console.log(JSON.parse(JSON.stringify(data)))

Chrome and Firefox at least will log a reference to an object if you just console.log(obj) .如果您只是console.log(obj) ,Chrome 和 Firefox 至少会记录对对象的引用 console.log(JSON.stringify(obj)) is going to convert your object to JSON which isn't an object. console.log(JSON.stringify(obj))会将您的对象转换为不是对象的 JSON。 console.log(JSON.parse(JSON.stringify(obj))) will convert the JSON back to an object as a separate, isolated reference that will not change. console.log(JSON.parse(JSON.stringify(obj)))将把 JSON 转换回一个对象,作为一个单独的、孤立的引用,不会改变。

 let data = { a: 'Tesla', b: 'Mclaren', c: 'Ferrari', d: 'Lamborghini', e: 'Lotus', 'f':{ name: 'John', 'e-mail': 'xyz@example.com', phone: '+12345678', country: 'USA', car: 'Toyota Prius' }, 'g':{ name: 'Sophie', 'e-mail': 'xyz@example.com', phone: '+12345678', country: 'UK', car: 'Nissan Bluebird' }, h: 'Volkswagen', i: 'Bugatti', j:[ '% mileage', '% top speed', '% suspension', '% navigation', '% horsepower', '% 0-60s' ] }; console.log(JSON.parse(JSON.stringify(data)));

You can use:您可以使用:

console.log(JSON.stringify(data));

OR要么

console.log(data);

If you are in the node environment, you can use the inspect method from the util module: https://nodejs.org/api/util.html#util_util_inspect_object_showhidden_depth_colors如果是node环境,可以使用util模块中的inspect方法: https ://nodejs.org/api/util.html#util_util_inspect_object_showhidden_depth_colors

const util = require('util');
console.log(util.inspect(bigObject, false, null, true)) ;

You can also have it log with colours, by setting the last argument to true like the above!您也可以像上面一样通过将最后一个参数设置为 true 来让它用颜色记录!

If you are in the browser environment, you can still use the util module, but you will have to use a bundler like webpack or browserify.如果你在浏览器环境下,你仍然可以使用 util 模块,但是你将不得不使用像 webpack 或 browserify 这样的捆绑器。

If you don't care about the output format console.log(JSON.stringify(data, null, 2)) should work fine如果您不关心输出格式console.log(JSON.stringify(data, null, 2))应该可以正常工作

If you iterate through an object that contains object instance then it will be logged as reference.If you want to iterate to throw the object in object use typeof while iterating.If it is object then iterate through the inner object.如果你迭代一个包含对象实例的对象,那么它将被记录为引用。如果你想迭代以在迭代时使用 typeof 将对象扔到对象中。如果它是对象,则迭代内部对象。

 let data = { a: 'Tesla', b: 'Mclaren', c: 'Ferrari', d: 'Lamborghini', e: 'Lotus', 'f':{ name: 'John', 'e-mail': 'xyz@example.com', phone: '+12345678', country: 'USA', car: 'Toyota Prius' }, 'g':{ name: 'Sophie', 'e-mail': 'xyz@example.com', phone: '+12345678', country: 'UK', car: 'Nissan Bluebird' }, h: 'Volkswagen', i: 'Bugatti', j:[ '% mileage', '% top speed', '% suspension', '% navigation', '% horsepower', '% 0-60s' ] } for(var key in data){ console.log(key + ": ", data[key]); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从javascript对象中删除所有嵌套的数组和对象 - How to remove all nested arrays and objects from javascript object 如何从嵌套的对象数组中删除对象属性? - How to remove object properties from nested arrays of objects? 如何循环和显示对象数组及其嵌套数组 - How to loop and display array of objects and their nested arrays 将 CSV 转换为 JSON,包括嵌套的对象数组 - Convert CSV to JSON including nested arrays of objects 如何发送嵌套的 object 对象数组,包括 FormData 中的文件? - How to send nested object with array of objects including files in FormData? Javascript - 遍历 Json 对象并获取每个项目的分层键,包括嵌套对象和数组 - Javascript - Traverse through the Json Object and get each item hierarchical key including nested objects and arrays (递归)如何从具有嵌套对象和数组的对象中获取所有键/值对 - (Recursion) How to get all key/value pairs from an Object with nested objects and arrays 从包含对象的嵌套数组数组中创建新对象 - Create new object from array of nested arrays that contains objects 从数组和对象的深度嵌套对象中返回所有 id - Return all id's from a deeply nested Object of arrays and objects 从嵌套对象内部的 arrays 获取值,比较并返回 object - Get values from arrays inside nested objects, compare and return the object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM