如何打印 javascript 对象?

发布于 2024-09-24 04:08:14 字数 781 浏览 2 评论 0原文

我有一个对象,

var object= {}

我在对象中放入一些数据,然后我想像这样打印它,

document.write(object.term);

该术语是一个根据不同情况而变化的变量。当我尝试打印它时,它出现未定义。

该怎么做呢?

更新:

这是我正在处理的代码。我想它可能与我上面所说的不一样,因为我是在 selenium 中使用 browsermob 进行的,我只是认为它与 document.write() 类似。这是代码

var numCardsStr = selenium.getText("//div[@id='set-middle']/div[2]/h2");

var numCards = numCardsStr.substr(4,2); 

browserMob.log(numCards);

var flash = {}

for(i=0; i<(numCards); i++){

var terms = selenium.getText("//div[@id='words-normal']/table/tbody/tr[" + (i + 2) + "]/td[1]");
var defs = selenium.getText("//div[@id='words-normal']/table/tbody/tr[" + (i + 2) + "]/td[2]");

flash[terms] = defs;

browserMob.log(flash.terms);

}

I have an object

var object= {}

I put some data in the object and then I want to print it like this

document.write(object.term);

the term is a variable that changes depending on different situations. When I try printing this it comes up with undefined.

How would it be done?

Update:

this is the code I am dealing with. I guess it probably isn't the same as what I said above because I am doing it in selenium with browsermob, I just thought it would be similar to document.write(). Here is the code

var numCardsStr = selenium.getText("//div[@id='set-middle']/div[2]/h2");

var numCards = numCardsStr.substr(4,2); 

browserMob.log(numCards);

var flash = {}

for(i=0; i<(numCards); i++){

var terms = selenium.getText("//div[@id='words-normal']/table/tbody/tr[" + (i + 2) + "]/td[1]");
var defs = selenium.getText("//div[@id='words-normal']/table/tbody/tr[" + (i + 2) + "]/td[2]");

flash[terms] = defs;

browserMob.log(flash.terms);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

弄潮 2024-10-01 04:08:15

如果您使用 document.write(),那么您很有可能在实例化对象之前尝试引用该对象。我的建议:不要使用 document.write() ,除非您在模板中需要它。对于所有其他目的,请等到页面加载,然后将脚本作为事件处理程序运行。

失败可能还有其他原因,但您的代码示例不够完整,无法进行诊断。

If you're using document.write(), there's a good chance you are trying to reference the object before it's been instantiated. My advice: don't use document.write() unless you need it in a template. For all other purposes, wait till the page loads and then run your script as an event handler.

There could be other reasons for the failure, but your code sample isn't complete enough for a diagnosis.

待天淡蓝洁白时 2024-10-01 04:08:15

要将整个对象输出为文本,请使用 JSON 库:

<script type="text/javascript" src="http://www.JSON.org/json2.js"></script>

var o = { "term": "value" };
document.write(JSON.stringify(o, null, 4));

这会将对象输出为字符串并缩进 4 个空格以方便阅读。


你要做的是这样的:

var terms = "abcd";
var defs = "1234";
var flash = {};
flash[terms] = defs;

这会创建这个对象:

{
    "abcd": "1234"
}

如果你想浏览属性(即“abce”),请执行以下操作:

for (var key in flash) {
    document.write('Key "' + key + '" has value "' + flash[key] + '"<br/>');
}

这将输出:

Key "abcd" has value "1234"

To output the whole object as text, use a JSON library:

<script type="text/javascript" src="http://www.JSON.org/json2.js"></script>

.

var o = { "term": "value" };
document.write(JSON.stringify(o, null, 4));

This will output the object as a string and indent 4 spaces to make it easy to read.


What you do is this:

var terms = "abcd";
var defs = "1234";
var flash = {};
flash[terms] = defs;

This creates this object:

{
    "abcd": "1234"
}

If you want to go through the properties (i.e. "abce"), do this:

for (var key in flash) {
    document.write('Key "' + key + '" has value "' + flash[key] + '"<br/>');
}

This will output:

Key "abcd" has value "1234"
烟酒忠诚 2024-10-01 04:08:15

因为我还没有看到提到这一点:

var a = {prop1:Math.random(), prop2:'lol'};
a.toString = function() {
    output = [];
    for(var name in this) if(this.hasOwnProperty(name) && name != 'toString') {
        output.push([name, this[name]].join(':'));
    }
    return "{\n"+output.join(",\n\t")+"\n}";
};
document.write(a);

// should look like:
/*
    {
        prop1:0.12134432,
        prop2:lol
    }
*/

如果您正在定义一个对象类,例如 MyObj

var MyObj = function(id) {
    this.someIdentity = id;
};
MyObj.prototype.toString = function() {
    return '<MyObject:'+this.someIdentity+'>';
};

然后每当您编写类似的内容时,

document.write(new MyObject(2));

它都会显示为

Because I haven't seen this mentioned yet:

var a = {prop1:Math.random(), prop2:'lol'};
a.toString = function() {
    output = [];
    for(var name in this) if(this.hasOwnProperty(name) && name != 'toString') {
        output.push([name, this[name]].join(':'));
    }
    return "{\n"+output.join(",\n\t")+"\n}";
};
document.write(a);

// should look like:
/*
    {
        prop1:0.12134432,
        prop2:lol
    }
*/

In the case that you're defining an object class, like MyObj:

var MyObj = function(id) {
    this.someIdentity = id;
};
MyObj.prototype.toString = function() {
    return '<MyObject:'+this.someIdentity+'>';
};

And then anytime you write something like

document.write(new MyObject(2));

It'll appear as <MyObject: 2>.

流殇 2024-10-01 04:08:15
  • 避免 document.write

  • 如果您使用 Firefox,请安装 firebug 并使用它的 控制台 api

  • 相同的控制台 api 也应该在 chrome 中工作。

  • 对于 IE,获取 companion js

  • 在 javascript 中,如果属性名称是事先已知的。如果不是,则:

如果 pn 包含属性名称,则 obj[pn] 应该为您提供值。

  • Avoid document.write

  • If you use Firefox, install firebug and use it's console api

  • The same console apis should work in chrome too.

  • For IE, get companion js

  • In javascript, obj.propertyname is used if the property name is known before hand. If it's not, then:

if pn contains the property name, obj[pn] should give you the value.

笨笨の傻瓜 2024-10-01 04:08:15

在 Firefox 和 Chrome/Safari 中,您可以简单地使用...

var myObj = {id: 1, name: 'Some Name'};
window.console.log(myObj);

控制台将输出类似“对象”的内容

。如果您在 Chrome 中,您可以使用内置的开发人员控制台轻松检查对象的内部值。

如果你使用 firefox,输出也应该来自 firebug...

我将其声明为使用 document.write 的替代方案,因为在文档上输出内容对我来说似乎有点侵入性...

Well in firefox and in Chrome/Safari you could simply use...

var myObj = {id: 1, name: 'Some Name'};
window.console.log(myObj);

And the console will output something like "Object"

If you are in Chrome you could inspect the internal values of the object with ease using the built in developer console.

If you use firefox the output should come out of firebug as well...

I'm stating this as an alternative of using document.write as it seems a little bit invasive to me to output content on the document...

盛夏已如深秋| 2024-10-01 04:08:14

编辑: 您使用了两个不同的变量名称:flashflashcards。我不知道它们是否意味着相同的东西,但是您正在使用 [] 表示法设置值,然后得到 它使用 . 表示法。

尝试:

var flash = {};

...

flash[terms] = defs;

browserMob.log(flash[terms]);

如果 term 是一个变量来表示您要检索的属性,那么您应该使用方括号表示法从对象中获取属性。

示例: http://jsfiddle.net/xbMjc/ (使用警报而不是 document.write)

var object= {};

object.someProperty = 'some value';

var term = "someProperty";

document.write( object[term] );  // will output 'some value'

EDIT: You're using two different variable names, flash and flashcards. I don't know if they are meant to be the same thing, but you are setting the value using the [] notation, then getting it using . notation.

Try:

var flash = {};

...

flash[terms] = defs;

browserMob.log(flash[terms]);

If term is a variable to represent the property you are retrieving, then you should use the square bracket notation for getting the property from the object.

Example: http://jsfiddle.net/xbMjc/ (uses alerts instead of document.write)

var object= {};

object.someProperty = 'some value';

var term = "someProperty";

document.write( object[term] );  // will output 'some value'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文