动态地将字符串评估为没有 eval() 的对象?

发布于 2024-09-10 07:03:29 字数 1775 浏览 2 评论 0原文

编辑:感谢肯尼和其他回答类似的人。这真的是一个DOH...太愚蠢了我真不记得这一点。

也许答案太简单了,我都想不起来……但我知道这里有人希望能教我这方面的知识。

所以我有一个相当大的项目,需要处理大量非常大的 JSON、对象、数组等。我需要一种动态访问这些数据的方法,而无需事先了解这些数据的实际名称。我知道 someobjectname[string] 有效,但我需要 [string][string][string] 等等。换句话说,整个事情必须是完全动态的。

我知道,我知道,这种方法存在性能问题,我确信有更好的方法,但我继承了这个问题并相信我,这不是改变它的选择。

现在,这是一个超级超级简化的示例来证明根本问题。我无法找到不使用 eval() 的方法,我无法使用它,因为数据不是来自可信来源。

在此示例中,假设 foo 和 bar(对象名称和相应的选项值)直到运行时才能知道。为了简单起见,我们假设它们是用您最喜欢的服务器端代码打印的。

<script>
// Pretend these objects are inserted into
// the DOM dynamically from where ever
// so we don't know the names till runtime
var foo = {
   value : "something"
}

var bar = {
   value : "something else"
}

window.onload = function() {

    function alertValue(option) {
                            // vvvv This is what I can't do
         var selected_object = eval(option.getAttribute("value"));
         var selected_value = selected_object.value;
         alert(selected_value);    
    }

    var option1 = document.getElementById("option1"); 
    var option2 = document.getElementById("option2");  

    option1.onclick = function () {
         alertValue(this);
    }

    option2.onclick = function () {
        alertValue(this);
    }

}

</script>

<html>
    <select>   <!-- Pretend these values are generated at runtime serverside -->
        <option id="option1" value="foo">Foo's value</option>
        <option id="option2" value="bar">Bar's value</option>
    </select>
</html>

任何帮助都会很棒。我希望这是一个简单的“DOH”时刻。请不要拆散这段代码或方法,因为没有意义。它并不像实际项目那么复杂。这只是一个概念证明,所以你会遇到问题。

EDIT: Thanks Kenny and everyone else who answered similarly. It really was a DOH...So stupid of me not to remember this.

Maybe the answer is so simple it's eluding me...but I know someone here can school me on this hopefully.

So I've got a rather large project that's dealing with tons of very large JSON, Objects, Arrays, etc..And I need a way of dynamically accessing this data without prior knowledge of the actual names of these. I know someobjectname[string] works, but I need [string][string][string] etc etc. In other words, the entire thing has to be completely dynamic.

I know, I know, there's performance issues with this approach and I'm sure there is better methods but I'm inheriting this problem and believe me, it's not an option to change it.

NOW, here's a super-uber over simplified example to prove the underlying problem. I can't find a way without using eval(), which I can't use because the data is NOT coming trusted sources.

In this example, pretend that foo and bar (both the object names and corresponding option values) are NOT able to known until runtime. Let's say for simplicity they're printed w/ your favorite server-side code.

<script>
// Pretend these objects are inserted into
// the DOM dynamically from where ever
// so we don't know the names till runtime
var foo = {
   value : "something"
}

var bar = {
   value : "something else"
}

window.onload = function() {

    function alertValue(option) {
                            // vvvv This is what I can't do
         var selected_object = eval(option.getAttribute("value"));
         var selected_value = selected_object.value;
         alert(selected_value);    
    }

    var option1 = document.getElementById("option1"); 
    var option2 = document.getElementById("option2");  

    option1.onclick = function () {
         alertValue(this);
    }

    option2.onclick = function () {
        alertValue(this);
    }

}

</script>

<html>
    <select>   <!-- Pretend these values are generated at runtime serverside -->
        <option id="option1" value="foo">Foo's value</option>
        <option id="option2" value="bar">Bar's value</option>
    </select>
</html>

Any help would be great. I'm hoping it's a simple "DOH" moment. Please don't rip apart this code or the method cause there's no point. It isn't anywhere near as complex as the real project is. It's just a proof of concept so you get the problem.

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

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

发布评论

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

评论(3

当爱已成负担 2024-09-17 07:03:29

如果您在浏览器中执行此操作,则可以使用 window 引用全局上下文,因此

var selected_object = window[option.getAttribute("value")];

(或者我不明白您的困难是什么。)

If you're executing this in the browser, the global context can be referred with window, hence

var selected_object = window[option.getAttribute("value")];

(or I don't understand what your difficulty is.)

心安伴我暖 2024-09-17 07:03:29

我不确定这是否是您想要的,但您也可以通过 window["foo"] 访问对象 foobar窗口[“bar”]

I am not sure if this is what you want, but you can access the objects foo and bar also via window["foo"] and window["bar"].

轮廓§ 2024-09-17 07:03:29

foo.barfoo["bar"] 在 Javascript 中含义相同。后者用于名称在运行时未知或作为标识符无效的属性。你可以轻松地链接它; foo["bar"]["baz"]foo.bar.baz 含义相同。

foo.bar and foo["bar"] mean the same thing in Javascript. The latter is used for properties whose names aren't known til runtime or aren't valid as identifiers. You can easily chain this; foo["bar"]["baz"] means the same as foo.bar.baz.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文